在我的模型中,我有:
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
public DateTime StartDate { get; set; }
不幸的是,它仅适用于 EditorFor,但我无法将 EditorFor 分配给类以启用 Jquery 日期时间选择器,因为没有 htmlAttributes
参数。
@Html.EditorFor(model => model.StartDate)
@Html.TextBoxFor(model => model.StartDate, new { @class = "datePicker", @readonly = "readonly" })
因此第一个仅显示日期,但没有日期选择器。 第二个也显示时间 00:00,但有一个日期时间选择器。
注意:不要只是说,使用model.StartDate.ToString("mm/DD/yyyy")
。它不起作用。
如何仅设置日期。但是有一个时间选择器并且是只读的,所以我无法在其中输入任何数字。
最佳答案
我正在使用 jquery UI 日期选择器,它不是附加类标签,而是通过以下方式设置默认日期格式:
$(function () {
$.datepicker.setDefaults({
dateFormat: 'dd/mm/yy'
});
});
然后将其绑定(bind)到输入元素:
$(function () {
$("#StartDate").datepicker();
});
希望有帮助。这就是 EditorFor mvc 标签上的内容。
关于jquery - ASP.NET MVC3 : Displaying Date in correct format with JQuery DatePicker and editorFor/TextBoxFor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9196281/