ASP.Net MVC model binding - how to change date format?

穿精又带淫゛_ 提交于 2019-11-30 14:04:54

I found what happended. My form was posting via GET method, and the MVC just uses the culture for an action parameter when it's passed in the RouteData or by the form via POST method.

I just changed the form to POST method and it worked.

Try this on your property in the view model:

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
The Internet

I'm pretty sure the issue is a lack of a DateTimeFormat on your DateTime types.

<input type="text" name="DateProperty" id="DateProperty"
       value="@(Model.DateProperty.Value.ToString("d",
             System.Threading.Thread.CurrentThread.CurrentUICulture.DateTimeFormat))" />

EDIT**

Another thing you want to be sure of is that the "name" property of your input element matches what you're passing into your action. If it doesn't null will appear on POST action every time.

     [HttpPost]
     public ActionResult DoStuff(string dateParam)
     {
       return RedirectToAction("Home","Index", new { });
     }

"dateParam" Should match the name property here.

<input id="dateParam" name="dateParam" value="10/10/2010" />
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!