Asp.net MVC and Asp.net webfrom have a very notable feature: the former removes all server controls, and the old days of pulling controls are no longer, replacing the client controls. You can create these client controls in two ways:
1. handwritten HTML control in view. The advantage is that it is very intuitive, but the disadvantage is thatCodeQuantity.
Second: Use htmlhelper under system. Web. MVC. html in the view to complete the client output of most controls.
Htmlhelper features: Let's take a look at its source code structure.
Namespace System. Web. MVC. html
{
Public Static Class Formextensions // Form-related extension methods, such as creating form labels.
Public Static Class Inputextensions // It contains all input values, such as text, button, and readiobutton.
Public Static Class Linkextensions // Link Methods
Public Class Mvcform: idisposable // Independent from client controls
Public Static Class Renderpartialextensions // This is the output partialview
Public Static Class Selectextensions // Output drop-down box
Public Static Class Textareaextensions // Output multi-line text box
Public Static Class Validationextensions // Verify the relevant form elements.
}
Example: Create a view of the messageIt is interesting that using is applied when creating a form, which is the same as C #, but its purpose is not to display the HTML call. the endform () method automatically adds the end tag at the end of the scope.
<% Using (Html. beginform ())
{ %>
< Fieldset >
< P >
< Label For = " Title " >
Title: </ Label >
<% = Html. Textbox ( " Stitle " , Model. stitle) %>
</ P >
< P >
< Label For = " Eventdate " >
Content: </ Label >
<% = Html. Textbox ( " Scontent " , Model. scontent) %>
</ P >
< P >
< Input type = " Submit " Onclick = " Return check (); " Value = " Save " />
</ P >
</ Fieldset >
<% }
%>
System. web. MVC. htmlhelper in HTML can only output most HTML controls, but IMG labels are not provided by default. here we need to expand helper by ourselves, after all, the above methods are extended.
Extended helper. We can use tagbuilder to output all labels and attributes. Tagbuilder provides the following important methods:
// Methods
Public Tagbuilder ( String Tagname );
Public Void Addcssclass ( String Value ); // Add Style
Public Void Generateid ( String Name ); // Set the Control ID
Private String Getattributesstring ();
Public Void Mergeattribute ( String Key, String Value ); // Set attribute values
Public Void Mergeattribute ( String Key, String Value, Bool Replaceexisting );
Public Void Mergeattributes < Tkey, tvalue > (Idictionary < Tkey, tvalue > Attributes );
Public Void Mergeattributes < Tkey, tvalue > (Idictionary < Tkey, tvalue > Attributes, Bool Replaceexisting );
Public Void Setinnertext ( String Innertext ); // Set display text
Public Override String Tostring ();
Public String Tostring (tagrendermode rendermode ); // Output Control html
1. Create an imagehelper and use the tagbuilder method to output the IMG tag.
Public Static Class Imagehelper
{
Public Static String Image ( This Htmlhelper helper, String ID, String URL, String Alternatetext)
{
Return Image (helper, ID, URL, alternatetext, Null );
}
Public static string image ( This htmlhelper helper, string ID, string URL, string alternatetext, Object
htmlattributes)
{< br> // Create an IMG tag
var builder = New tagbuilder ( " IMG " );
// Add ID attributes
builder. generateid (ID);
// Add attributes
builder. mergeattribute ( " SRC " , URL );
builder. mergeattribute ( " alt " , alternatetext );
builder. mergeattributes ( New routevaluedictionary (htmlattributes ));
//Output complete IMG labels
ReturnBuilder. tostring (tagrendermode. selfclosing );
}
}
2: Page call.
<% = Html. Image ( " Img1 " , " Http://a.lakequincy.com/img/633820582974214892.jpg " , " This is an image. " , New
{border = " 4px " }) %>
conclusion: with System. web. MVC. HTML and tagbuilder, page control output has all been solved.
Note: Reference: http://www.asp.net/learn/mvc/tutorial-35-cs.aspx