The first day of New Year's work, write a blog opening!
In MVC, auxiliary classes are very common, for example, Html.textbox (), Html.dropdownlistfor (), and so on, which Microsoft helps us encapsulate, can be called directly, they parse out the results are a piece of Html code, and the actual project, We may need to expand some of the auxiliary classes we need, such as the following code:
@UserHelpers. Usermsg (this. Html, Viewbag.msg, Viewbag.msgtype), which functions as a system message prompt.
Let's go to its definition to see:
1 Public StaticMvchtmlstring Usermsg ( ThisHtmlHelper HTML,stringMsgstringMsgtype)2 {3Tagbuilder TB =NewTagbuilder ("Div");4Tb. addCssClass ("Alert Divshadow"+msgtype);5Tb. Generateid ("alertmsg");6 7Tagbuilder tagItem1 =NewTagbuilder ("a");8Tagitem1.addcssclass ("Close");9TAGITEM1.ATTRIBUTES.ADD ("href","#");TenTAGITEM1.ATTRIBUTES.ADD ("Data-dismiss","Alert"); OneTagitem1.setinnertext ("x"); ATb. InnerHtml + =tagitem1.tostring (); - -Tagbuilder tagItem2 =NewTagbuilder ("Strong"); theTagitem2.generateid ("msgcontent"); - Tagitem2.setinnertext (msg); -Tb. InnerHtml + =tagitem2.tostring (); - + returnmvchtmlstring.create (TB. ToString ()); -}
View Code
Userhelper is the auxiliary class name, Usermsg is the auxiliary method name, it includes three parameters, the first one is the system default, the latter two we wrote in. Also, to generate HTML code in view, we need to specify that the return type of the helper method is mvchtmlstring.
In-Method code interpretation:
Tagbuilder is used to generate HTML tags.
Of course, what you want to see in the view, you need to do the stitching.
Finished, write well simple, haha, wish oneself in the new year work smoothly!
MVC4.0 Extended Helper Method