Asp.net MVC provides a large number of helper, such as the following helper:
Public Static String Actionlink ( This Htmlhelper, String Linktext, String Actionname, Object Routevalues, Object Htmlattributes)
In actual development, we cannot avoid extending helper, for example, extending a link method (MVC only provides actionlink and routelink, but does not extend the most common link)
Public Static String Link ( This Htmlhelper, String Linktext, String Linkhref, Object Htmlattributes)
{
.
}
There is an htmlattribute, which is the so-called "anonymous type" in C #3.0. It allows the user to write the following when calling this method:
<% = Html. Link ( " Caraq " , " Http://www.caraq.com " , New {@ Class = " Home " , Target = " _ Blank " }) %>
In the link method, how does one know the HTML attributes and values specified by the user?
After checking the MVC source code, we found that it uses a routevaluedictionary class defined in the system. Web. Routing component. Therefore, you can obtain the user-specified HTML attribute values as follows:
Public Static String Link ( This Htmlhelper, String Linktext, String Linkhref, Object Htmlattributes)
{
String Reval = " <A href = \ " { 0 }\ " {1 }>{ 2 }</A> " ;
Stringbuilder = New Stringbuilder ();
Foreach (VAR item In New Routevaluedictionary (htmlattributes ))
{
Builder. append (item. Key + " = \ "" + Item. Value + " \ " " );
}
Return String . Format (reval, linkhref, builder. tostring (), linktext );
}