The my97date used in the project, the other fields are bound with the @Html. Editorfor (model = model. field), but the calendar control is bound with <input/>, but it is not particularly good to write.
In order to also be able to bind attributes with @html, an extension method is used. The code is as follows:
Public Static classMy97datepicker {Private Static stringDefaultFormat ="YYYY-MM-DD"; /// <summary> ///to build a control with a specific name/// </summary> /// <param name= "helper" >HtmlHelper Object</param> /// <param name= "name" >Control Name</param> /// <returns>HTML text</returns> Public StaticMvchtmlstring Calendar ( ThisHtmlHelper Helper,stringname) { returnCalendar (helper, name, DefaultFormat); } /// <summary> ///to build a control with a specific name/// </summary> /// <param name= "helper" >HtmlHelper Object</param> /// <param name= "name" >Control Name</param> /// <param name= "format" >display Format</param> /// <returns>HTML text</returns> Public StaticMvchtmlstring Calendar ( ThisHtmlHelper Helper,stringNamestringformat) { returnGeneratehtml (Name,NULL, format); } /// <summary> ///to build a control with a specific name and initial value/// </summary> /// <param name= "helper" >HtmlHelper Object</param> /// <param name= "name" >Control Name</param> /// <param name= "Date" >date and time to display</param> /// <returns>HTML text</returns> Public StaticMvchtmlstring Calendar ( ThisHtmlHelper Helper,stringname, DateTime date) { returnCalendar (helper, name, date, DefaultFormat); } /// <summary> ///to build a control with a specific name and initial value/// </summary> /// <param name= "helper" >HtmlHelper Object</param> /// <param name= "name" >Control Name</param> /// <param name= "Date" >date and time to display</param> /// <param name= "format" >display Format</param> /// <returns>HTML text</returns> Public StaticMvchtmlstring Calendar ( ThisHtmlHelper Helper,stringName, DateTime date,stringformat) { returngeneratehtml (name, date, format); } /// <summary> ///building controls with lambda expressions/// </summary> /// <param name= "helper" >HtmlHelper Object</param> /// <param name= "expression" >A lambda expression that specifies the property to display and the object to which it belongs</param> /// <returns>HTML text</returns> Public StaticMvchtmlstring Calendarfor<tmodel, Tproperty> ( ThisHtmlhelper<tmodel> Helper, Expression<func<tmodel, tproperty>>expression) { returncalendarfor (helper, expression, DefaultFormat); } /// <summary> ///building controls with lambda expressions/// </summary> /// <param name= "helper" >HtmlHelper Object</param> /// <param name= "expression" >A lambda expression that specifies the property to display and the object to which it belongs</param> /// <param name= "format" >display Format</param> /// <returns>HTML text</returns> Public StaticMvchtmlstring Calendarfor<tmodel, Tproperty> ( ThisHtmlhelper<tmodel> Helper, Expression<func<tmodel, tproperty>> Expression,stringformat) { stringName =expressionhelper.getexpressiontext (expression); DateTime value; Objectdata = Modelmetadata.fromlambdaexpression<tmodel, tproperty>(expression, helper.) ViewData). Model; if(Data! =NULL&& datetime.tryparse (data. ToString (), outvalue)) { returngeneratehtml (name, value, format); } Else { returnGeneratehtml (Name,NULL, format); } } /// <summary> ///get the DateTime to display through a lambda expression/// </summary> /// <param name= "helper" >HtmlHelper Object</param> /// <param name= "expression" >A lambda expression that specifies the property to display and the object to which it belongs</param> /// <param name= "format" >display Format</param> /// <returns>HTML text</returns> Public StaticMvchtmlstring Calendardisplayfor<tmodel, Tproperty> ( ThisHtmlhelper<tmodel> Helper, Expression<func<tmodel, tproperty>> Expression,stringformat) { stringName =expressionhelper.getexpressiontext (expression); DateTime value; Objectdata = Modelmetadata.fromlambdaexpression<tmodel, tproperty>(expression, helper.) ViewData). Model; if(Data! =NULL&& datetime.tryparse (data. ToString (), outvalue)) { return Newmvchtmlstring (value. ToString (format)); } Else { return NewMvchtmlstring (""); } } /// <summary> ///get the DateTime to display through a lambda expression/// </summary> /// <param name= "helper" >HtmlHelper Object</param> /// <param name= "expression" >A lambda expression that specifies the property to display and the object to which it belongs</param> /// <returns>HTML text</returns> Public StaticMvchtmlstring Calendardisplayfor<tmodel, Tproperty> ( ThisHtmlhelper<tmodel> Helper, Expression<func<tmodel, tproperty>>expression) { returncalendardisplayfor (helper, expression, DefaultFormat); } /// <summary> ///generate HTML for the input box/// </summary> /// <param name= "name" >Name of Calendar</param> /// <param name= "Date" >the value of the calendar</param> /// <returns>HTML text</returns> Private StaticMvchtmlstring generatehtml (stringName, DateTime? Datestringformat) { stringstr =""; if(Date! =NULL) {str="<input type=\ "text\" id=\ ""+ name +"\ "Onclick=\" Wdatepicker ({el: '"+ name +"'}) \ "Class=\" wdate\ "value=\""+ Date. Value.tostring (format) +"\ "/>"; } Else{str="<input type=\ "text\" id=\ ""+ name +"\ "Onclick=\" Wdatepicker ({el: '"+ name +"'}) \ "Class=\" wdate\ "value=\" \ "/>"; } return Newmvchtmlstring (str); } }
As long as we refer to the namespace of this method, we can use @html.calendarfor and other methods happily. Of course also need to refer to My97date JS file.
My97date applying C # extension methods in MVC