Format Date On Binding (ASP.NET MVC)

后端 未结 12 725
感情败类
感情败类 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:23

    In order to get strongly typed access to your model in the code behind of your view you can do this:

    public partial class SomethingView : ViewPage
    {
    }
    

    Where T is the ViewData type that you want to pass in from your Action.

    Then in your controller you would have an action :

    public ActionResult Something(){
        T myObject = new T();
        T.Property = DateTime.Today();
    
        Return View("Something", myObject);
    }
    

    After that you have nice strongly typed model data in your view so you can do :

    
    <%=Html.TextBox(ViewData.Model.Property.ToShortDateString())%>
    

提交回复
热议问题