具有默认日期的MVC4日期选择器 [英] MVC4 Date Picker with Default Date

查看:99
本文介绍了具有默认日期的MVC4日期选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将MVC4与Razor视图引擎一起使用.我的控制器如下:

I am using MVC4 with the Razor view engine. My controller is as follows:

public ActionResult Index()
{

    return View(new EmployeeIndexViewModel
    {

        ToDate = DateTime.Now

    });

}

我的视图模型如下:

public class EmployeeIndexViewModel
{
    [DataType(DataType.Date)]
    public DateTime ToDate { get; set; }
}

在我看来,我有一个简单的EditorFor()呼叫:

On my view I have a simple EditorFor() call:

@Html.EditorFor(model => model.ToDate)

但在日期选择器中仍会得到默认值(带掩码):

But still get a default (masked) value in my date picker:

问题:

如何从控制器获取今天的日期呢?

How can I get today's date (from controller) to display here instead?

推荐答案

我的问题不是我最初想到的jQuery或MVC4问题.它与HTML5兼容浏览器如何显示日期选择器有关.我将日期以错误的格式传递给视图.

My problem was not a jQuery or MVC4 problem as I had initially thought. It has to do with how the HTML5 compatible browser displays the date picker. I was passing the date to the view in the incorrect format.

我将ViewModel修改为此,现在正确填充了日期:

I modified my ViewModel to this and now the date populates correctly:

public class EmployeeIndexViewModel
{
    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
    public DateTime ToDate { get; set; }
}

这篇关于具有默认日期的MVC4日期选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆