Format Date with culture in Razor @Html.EditFor

独自空忆成欢 提交于 2020-01-04 21:36:05

问题


MVC 4, C#.

In my razor view I have:

@Html.EditorFor(model => model.ActionObject.StartDate)

Which is annotated in my model with:

[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public System.DateTime? StartDate { get; set; }

In the view, it is set like so, with date picker:

Notice how it is mm/dd/yyyy. How can I change this dynamically based on the user? The user can set there culture to whatever they want.

Normally I can do:

.ToString("d", culture)

Where culture is set dynamically. But this doesn't work for @Html.Editfor.


回答1:


This datepicker is the native representation for HTML 5 input type date in Google Chrome. You can't change this format. The display format comes from user Culture / Localization config in his OS.

If you want to format this as you want, remove the [DataType(DataType.Date)], rendering a default input field and then use a JQuery UI DatePicker or another one to render a better date field.



来源:https://stackoverflow.com/questions/19283557/format-date-with-culture-in-razor-html-editfor

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