My97date applying C # extension methods in MVC

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.