MVC4.0 Extended Helper Method

Source: Internet
Author: User

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

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.