Format Date On Binding (ASP.NET MVC)

后端 未结 12 731
感情败类
感情败类 2021-01-31 08:46

In my ASP.net MVC app I have a view that looks like this:

...

<%=Html.TextBox(\"due\")%>
...

I am usi

12条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-31 09:22

    I just came across this very simple and elegant solution, available in MVC 2:

    http://geekswithblogs.net/michelotti/archive/2010/02/05/mvc-2-editor-template-with-datetime.aspx

    Basically if you are using MVC 2.0, use the following in your view.

     <%=Html.LabelFor(m => m.due) %>
     <%=Html.EditorFor(m => m.due)%>
    

    then create a partial view in /Views/Shared/EditorTemplates, called DateTime.ascx

    <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
    <%=Html.TextBox("", (Model.HasValue ? Model.Value.ToShortDateString() : string.Empty), new { @class = "datePicker" }) %>
    

    When the EditorFor<> is called it will find a matching Editor Template.

提交回复
热议问题