关于ASP.NET MVC:ASP MVC EditorFor DateTime不显示

asp mvc EditorFor DateTime not displaying

我试图显示从剃刀文件中的数据库检索到的日期时间字段,内容如下:

1
 @Html.EditorFor(model => model.RequestDate);

我以100%的确定性知道RequestDate不为null,因为我检查了在视图中传递的模型,它是来自数据库的日期。 但是,日期时间文本框仍显示" mm / dd / yyyy"。 我不知道为什么它不显示日期。 有任何想法吗? 谢谢。

这是我的模型:

1
2
3
      [Display(Name ="Time")]
    [DataType(DataType.Date)]
    public DateTime RequestDate { get; set; }

编辑:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
   [HttpGet]
    public ActionResult DispatchResponseIndex()
    {
        DispatchResponseModel model = new DispatchResponseModel();      


        //if the users session id isnt null then we know they have data related to this form
        object UserID = Session["TestID"];
        if (UserID == null)
        {
            return View(model); //default view              
        }
        else
        {
            UserID = Session["TestID"];
        }

        LoadDB(model, (Guid)UserID);
     //looking at the model here the RequestDate has a valid date of  //{5/24/2013 12:00:00 AM}

        return View(model);
    }

编辑2:

如果我使用@ Html.TextBox(" asdasd",Model.RequestDate); 然后它将以字符串格式显示保存的日期,但是我需要为文本框使用内置的日期编辑器。


这似乎确实引起了很多混乱,这让我很烦!

如果使用DataType.Date,则需要这样设置:

1
2
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString ="{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]

Chrome的日期选择器使用本地计算机的短日期格式。即使设置lang标记也将被忽略;这是使用Chrome和的示例:

enter image description here

Chrome的日期选择器还使用本地计算机的语言环境。在页面加载时,将填充日期(如果已设置),并且用户可以选择新日期:

enter image description here

注意:如果您更改计算机的语言环境,则必须完全重启Chrome才能看到语言环境的变化。


最后

我是个白痴。我不知道Chrome是否添加了日期选择器。我认为这是MVC内置的东西。我需要使用实际的jQuery日期选择器。感谢大家提供的所有信息。


在模型中添加格式行

1
2
3
4
[Display(Name ="Time")]        
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString ="{0:MM/dd/yyyy}")]
[DataType(DataType.Date)]
public DateTime RequestDate { get; set; }

更新资料

要么

您可以将模型属性datetime更改为string吗?并将您的日期时间对象转换为字符串!

要么

或者您可以在编辑器字段中格式化该值,例如

1
@Html.TextBoxFor(model => model.FromDate,"{0:d}")


实际上,直到我看了这篇文章,我才意识到Chrome和IE之间没有"日期选择器"的区别,即使我从MVC教程中获得了示例页面布局。

对于像我之前一样没有意识到差异的人,请在下面的比较中找到:

enter image description here


这就是我在模型内做的方法

1
2
3
4
5
6
7
8
9
10
11
12
13
[Display(Name ="Effective Date")]
    [DataType(DataType.Date)]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString ="{0:dd-MMM-yyyy}")]
    public DateTime? Effectivedate
    {
        get { return EffectivedateEdit; }
        set { EffectivedateEdit = value; EffectivedateEdit = value; }
    }


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

我有这个完全相同的问题。如果您查看返回到Controller的HTTP Post以及POST中日期的格式,则为yyyy-MM-dd。

我将数据注释更改为此,并且它起作用了……除了现在的索引列表视图显示了yyyy-mm-dd格式。而Edit在datepicker控件中显示mm / mm / yyyy?

已添加到模型日期-

1
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString ="{0:yyyy-MM-dd}")

自1980年以来,我见过的恕我直言的每个应用程序都使用日期和时间。 MS先生,为什么这还是一团糟?