ASP. NET mvc3 HTML Tag Extension

Source: Internet
Author: User

I recently studied this. I think it is okay to extend the HTML Tag in this project when I used Java. I checked the relevant information for successful calling. I will send it to you to see it, making progress together also prevents you from forgetting it ~


Let's talk about the Code directly:


Using system; <br/> using system. collections. generic; <br/> using system. runtime. compilerservices; <br/> using system. LINQ. expressions; <br/> using system. web. MVC; <br/> namespace system. web. MVC <br/>{< br/> Public static class htmlextensions <br/>{< br/> Public static mvchtmlstring mytag (this htmlhelper HTML, string name, string id = "") <br/>{< br/> string innerhtml = string. format ("My name is: {0 }" , Name); <br/> tagbuilder = new tagbuilder ("span"); <br/> tagbuilder. innerhtml = innerhtml; <br/> If (! String. isnullorempty (ID) <br/>{< br/> tagbuilder. attributes. add ("ID", ID); <br/>}< br/> return mvchtmlstring. create (tagbuilder. tostring (); <br/>}< br/> 

Note that the namespace should be system. web. MVC, because the htmlhelper class is in this, which should be used here. the Extension Method in net 3.0 implements HTML Tag extension. The mvchtmlstring object is used to make the output tag conform to HTML. If the string is output directly, when the page is displayed, all the content of the label is displayed. For example, the above example is changed to <span id = "id you passed"> name </span>.


The call at the view layer is as follows:



@ Inherits system. Web. MVC. webviewpage <br/>{< br/> View. Title = "home page"; <br/> layoutpage = "~ /Views/shared/_ layout. cshtml "; <br/>}< br/> <H2> @ view. message </H2> <br/> <MCE: style> <! -- <Br/> # test {<br/> color: # ff0000; <br/>}< br/> --> </MCE: style> <style mce_bogus = "1" >#test {<br/> color: # ff0000; <br/>}</style> <br/> @ HTML. mytag ("test", "test") <br/> <p> <br/> to learn more about ASP. net MVC visit <a href = "" mce_href = "" Title = "ASP. net MVC Website "> </a>. <br/> </P> <br/>


Shows the effect:




In this way, you can basically implement the custom tag library, implement or encapsulate some space or functions you need.


The record is for memo and also for sharing some experience.


For the above example, debug the mvc3 project under vs2010.

Related Article

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: 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.