MVC4 extension methods

Source: Internet
Author: User

1. Extension Code Institutions

namespacesystem.web.mvc.html{ Public Static classFormExtensions//form-related extension methods, such as creating form labels, and so on.      Public Static classInputExtensions//all input is included here, for example: Text,button,readiobutton and so on.      Public Static classLinkextensions//Link Related methods     Public classMvcform:idisposable//independent of client controls     Public Static classRenderPartialExtensions//This is the output Partialview     Public Static classSelectextensions//output drop-down box     Public Static classTextAreaExtensions//output multi-line text box     Public Static classValidationExtensions//output related FORM element validation. }

2. Tagbuilder structure

    //Methods     PublicTagbuilder (stringtagName);  Public voidaddCssClass (stringValue);//Add Style     Public voidGenerateid (stringname);//set the control ID    Private stringgetattributesstring ();  Public voidMergeattribute (stringKeystringValue);//Setting property values     Public voidMergeattribute (stringKeystringValueBOOLreplaceexisting);  Public voidMergeattributes<tkey, Tvalue> (Idictionary<tkey, tvalue>attributes);  Public voidMergeattributes<tkey, Tvalue> (Idictionary<tkey, tvalue> attributes,BOOLreplaceexisting);  Public voidSetinnertext (stringInnerText);//Set display text     Public Override stringToString ();  Public stringToString (Tagrendermode rendermode);//Output Control HTML

3. Example: Html.image (...)

 Public Static classimagehelper{ Public Static stringImage ( ThisHtmlHelper Helper,stringIdstringUrlstringAlternateText) {            returnImage (helper, id, url, alternatetext,NULL); }         Public Static stringImage ( ThisHtmlHelper Helper,stringIdstringUrlstringAlternateText,Objecthtmlattributes) {            //Create an IMG tag            varBuilder =NewTagbuilder ("img"); //Add id attributeBuilder.            Generateid (ID); //Add PropertyBuilder. Mergeattribute ("src", URL); Builder. Mergeattribute ("alt", AlternateText); Builder. Mergeattributes (Newroutevaluedictionary (htmlattributes)); //output the full img tag            return builder.        ToString (tagrendermode.selfclosing); }}

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.