使用编辑器模板MVC进行日期时间格式化

Tsa*_*arl 2 c# asp.net-mvc-2

我想格式化TextBoxes中显示的DateTime,没有时间和格式:2011年1月1日,而不是显示和编辑方案的默认01/01/2011.

我已经使用DateTime的以下模板来使用datepicker.我可以在某种程度上包括格式吗?

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<System.DateTime?>" %>
<%: Html.TextBox("",ViewData.TemplateInfo.FormattedModelValue, new { @class = "text-box single-line" }) %>
Run Code Online (Sandbox Code Playgroud)

提前致谢.

Dar*_*rov 5

<%= Html.TextBox("",  Model.HasValue ? Model.Value.ToString("dd MMM, yyyy") : "", new { @class = "text-box single-line" }) %>
Run Code Online (Sandbox Code Playgroud)

或者更好的是在视图模型上:

[DisplayFormat(DataFormatString = "{0:dd MMM, yyyy}", ApplyFormatInEditMode = true)]
public DateTime? Foo { get; set; }
Run Code Online (Sandbox Code Playgroud)