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); }}