1, what is the HtmlHelper auxiliary method? is actually the extension method of the HtmlHelper class, as follows:
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. }
For example, the extended class INPUTEXTENSIONS,MVC framework itself has an extension for this:
namespace system.web.mvc.html{ // Summary: // represents support for HTML input controls in an application. Public Static class inputextensions { public static mvchtmlstring CheckBox (The Thisstring name);
}
}
2, through the expansion of HtmlHelper to build their own htmlhelper auxiliary method
System.Web.Mvc.Html under the HtmlHelper can only complete most of the HTML control output, some of the things we often use it does not, how to do? Do It yourself ~
Before we expand, there's a class called Tagbuilder (Generate tags) that works better, and you don't have to dwell on its details, as long as you know that he has those 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
Now it's time to start expanding!
A, extended img Tag
namespacesystem.web.mvc{ Public Static classimageextensions { 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 returnBuilder. ToString (tagrendermode.selfclosing); } }}
Call: @Html. Image ("Img1", http://img/111.jpg, "This is a picture", new {border= "4px"})
Output: http://img/111.jpg" style= "BORDER:4PX;" alt= "This is a picture"/>
B, Extended div tag
namespacesystem.web.mvc{ Public Static classdivextensions { Public StaticString Div ( ThisHtmlHelper Helper, string ID, string content, String CSSStyle,Objecthtmlattrs) {Tagbuilder Builder=NewTagbuilder ("Div"); //Replace "." To "_"Builder. Idattributedotreplacement ="_"; Builder. Generateid (ID); Builder. Mergeattributes (Newroutevaluedictionary (htmlattrs)); Builder. addCssClass (CSSStyle); Builder. InnerHtml=content; returnBuilder. ToString (Tagrendermode.normal);//represents a double-sided label } }}
Call:
@Html. Div ("Mydiv.1", "extension method", "Myclassstyle", new {style= "Border:solid red 1px;"})
Output:
<div id= "mydiv_1" class= "Myclassstyle" style= "Border:solid red 1px;" > Extension Methods </div>
C. Extending the Span label
namespacesystem.web.mvc{ Public Static classspanextensions { Public Static stringSpan ( ThisHtmlHelper Helper,stringIdstringTextstringCssObjecthtmlattributes) { //Creative Tagbuilder of a certain tag varBuilder =NewTagbuilder ("span"); //Create the ID, and note that you must first set the Idattributedotreplacement property before you execute the Generateid method.Builder. Idattributedotreplacement ="-"; Builder. Generateid (ID); //Add PropertyBuilder. Mergeattributes (Newroutevaluedictionary (htmlattributes)); //Add StyleBuilder. addCssClass (CSS); //or in the form of the following sentence: Builder. Mergeattribute ("class", CSS); //add content, the following two ways can be//Builder. InnerHtml = text;Builder. Setinnertext (text); //Output Controls returnBuilder. ToString (Tagrendermode.normal); } }}
Call:
@Html. Span ("Span.test", "use Tagbuilder to help build extension method", "colorred", new {style="font-size:15px;"})
Output:
<span id= "span-test" class= "colorred" style= "font-size:15px;" > Using Tagbuilder to help build extension methods </span>
D, extended UL, Li label
namespacesystem.web.mvc{ Public Static classulliextensions { Public StaticMvchtmlstring Ulli ( ThisHtmlHelper Helper,string[] listItems) {Tagbuilder Ultag=NewTagbuilder ("ul"); foreach(stringIteminchListItems) {Tagbuilder Litag=NewTagbuilder ("Li"); Litag.setinnertext (item); Ultag.innerhtml+=litag.tostring (); } return Newmvchtmlstring (ultag.tostring ()); } }}
Call:
@Html. List (New string[]{"Apple", "grapefruit", "Strawberry", "Dragon Fruit"})
Output:
<ul> <li> Apple </li> <li> grapefruit </li> <li> strawberry </li> <li> dragon Fruit </li> </ul>
E, Extended intercept string (one field is too long to be intercepted when displayed)
namespacesystem.web.mvc{ Public Static classcutstringextensions { Public Static stringCutstring ( ThisSystem.Web.Mvc.HtmlHelper Helper,stringContentintlength) { if(content. Length >length) { returnContent. Substring (0, length) +"..."; } Else { returncontent; } } }}
Here is just a point, more usage to be based on actual needs to develop ~