jQuery DatePicker MVC4 EditorFor

后端 未结 3 954
盖世英雄少女心
盖世英雄少女心 2021-01-02 10:17

I need to use datepicker on an EditorFor field.

My code for the view is:

@Html.Label(\"birthDate\",\"birthDate
3条回答
  •  没有蜡笔的小新
    2021-01-02 11:09

    @Html.EditorFor(model => model.birthDate, new { @class = "datepicker"})
    

    Will not work, because "EditorFor" will not accept (pass along to the DOM) a Class.

    So, you need to use "TextBoxFor" which will accept a Class or better yet, create an EditorTemplate to handle any field that is a DateTime and add the class there. This link details how to make the Template.

    Once you have the Class working (can be verified in the source code), the rest is just referencing the jquery code. I use

    @Styles.Render("~/Content/themes/base/css")
    @Scripts.Render("~/bundles/jqueryval")
    @Scripts.Render("~/bundles/jqueryui")
    

    and activating it with the script you already have. Watch that the name '.datepicker' is Exactly the same as the Class.

提交回复
热议问题