ASP.NET MVC3:使用JQuery DatePicker和editorFor / TextBoxFor以正确的格式显示日期

ASP.NET MVC3: Displaying Date in correct format with JQuery DatePicker and editorFor/TextBoxFor

在我的模型中,我有:

1
2
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString ="{0:dd/MM/yyyy}")]
    public DateTime StartDate { get; set; }

不幸的是,它仅适用于EditorFor,但是由于没有htmlAttributes参数,因此我无法分配EditorFor类来启用Jquery Datetime选择器。

1
2
@Html.EditorFor(model => model.StartDate)
@Html.TextBoxFor(model => model.StartDate, new { @class ="datePicker", @readonly ="readonly" })

因此,第一个仅显示日期,但没有日期选择器。
第二个也显示时间00:00,但有一个datetimepicker。

注意:不要只说使用model.StartDate.ToString("mm/DD/yyyy")。 这没用。

如何设置日期。 但是有一个时间选择器并且具有只读的,所以我不能在其中键入任何数字。


我正在使用jquery UI datepicker,而不是附加类标签,而是通过以下方式设置默认日期格式:

1
2
3
4
5
    $(function () {
        $.datepicker.setDefaults({
            dateFormat: 'dd/mm/yy'
        });
    });

然后将其绑定到输入元素:

1
2
3
    $(function () {
        $("#StartDate").datepicker();
    });

希望能有所帮助。 多数民众赞成在EditorFor mvc标签上。