Format Date On Binding (ASP.NET MVC)

后端 未结 12 826
感情败类
感情败类 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条回答
  •  萌比男神i
    2021-01-31 09:14

    MVC4 EF5 View I was trying to preload a field with today's date then pass it to the view for approval.

    ViewModel.SEnd = DateTime.Now    //preload todays date  
    return View(ViewModel)           //pass to view
    

    In the view, my first code allowed an edit:

    @Html.EditedFor(item.SEnd)        //allow edit
    

    Later I changed it to just display the date, the user cannot change it but the submit triggers the controller savechanges

     
     @Html.DisplyFor(item.SEnd)       //show no edit
     
    

    When I changed to DisplayFor I needed to add this to ensure the preloaded value was passed back to the controller. I also need to add HiddenFor's for every field in the viewmodel.

        @Html.HiddenFor(model => model.SEnd)     //preserve value for passback.
    

    Beginners stuff but it took a while to work this out.

提交回复
热议问题