问题在Edit()中获取日期格式在ASP.NET MVC 3中

时间:2012-03-27 07:09:52

标签: asp.net-mvc-3 datetime date-format

当我在编辑用户注册表单时编辑我的一个表单ASP.NET MVC 3时,我在startdate中的日期是21-Mar-12 12:00:00 AM在文本框中,但我需要21-Mar-12

那么如何设置文本框日期呢?

1 个答案:

答案 0 :(得分:1)

您可以使用[DisplayFormat]属性修饰视图模型上的DateTime属性,该属性允许您指定给定的格式:

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd-MMM-yy}")]
public DateTime SomeDate { get; set; }

并在强类型视图中使用EditorFor帮助器呈现相应的输入字段:

@Html.EditorFor(x => x.SomeDate)

关于在回发时将值绑定回DateTime字段,您可以编写将使用此格式的自定义模型绑定器。我已经展示了example here