You'll want to use the HtmlHelper overload that allows you to specify Html attributes. Then target them with the jquery selector.
// in view
<%= Html.TextBoxFor(x => x.Date, new { @class="datepicker" })%>
// in js
$(document).ready({
$('.datepicker').datepicker();
});
Though I recommend using EditorFor
instead.
<%= Html.EditorFor(x => x.Date)%>
You can create an EditorTemplate to handle any field that is a DateTime
and have it output the proper field.
Create /Views/Shared/EditorTemplates/DateTime.ascx
and put this in it...
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<System.DateTime>" %>
<%= Html.TextBox("", Model.ToShortDateString(), new { @class="datepicker" } )%>
Or if you need to allow DateTime's with nulls:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<System.DateTime?>" %>
<%= Html.TextBox("", (Model.HasValue? Model.Value.ToShortDateString():""), new { @class="datepicker"} )%>
Then you can always use EditorFor and have a central ascx to edit if you ever want to modify the way date times are handled in your views.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…