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 = "http://asp.net/mvc" mce_href = "http://asp.net/mvc" Title = "ASP. net MVC Website "> http://asp.net/mvc </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.