ArticleDirectory
- Antiforgerytoken
- Default items added to dropdownlist
Download:Http://www.codeplex.com/aspnet/Release/ProjectReleases.aspx? Releaseid = 16775
Asp.net MVC codeplex preview 5YesAsp.net MVCIt contains some new features.
Pv5ForHelperThe changes are basically concentrated inHtmlhelper. For simple use, I have been around. If you don't understand anything, you can leave a message.
AttributeencodeProvides the htmlattributeencode Function
Usage
<% = Html. attributeencode ( " <SCRIPT src = \ " J. js \ " > </SCRIPT> <Div background = 'javascript: Alert (''); '/> " ) %>
Encoding result
& Lt; script SRC = & Quot; J. js & Quot; > & Lt; / Script > & Lt; Div background = ' Javascript: Alert ( '' ); ' />
The method only converts quotation marks (")," and "(&), and left angle brackets (<) into equivalent character entities. This method is much faster than the encode/htmlencode method.
EncodeProvides the htmlencode function.
Usage
<% = Html. encode ( " <SCRIPT src = \ " J. js \ " > </SCRIPT> <Div background = 'javascript: Alert (''); '/> " ) %>
Encoding result
& Lt; script SRC = & Quot; J. js & Quot; & GT; & Lt; / Script & GT; & Lt; Div background = ' Javascript: Alert ( '' ); ' /& GT;
RenderusercontrolChanged to renderpartial and provided a better way to search for templates.
<% = Html. renderusercontrol ( " /Views/shared/menu. ascx " ) %>
Change
<% Html. renderpartial ( " Menu " ); %>
Note that the original <% = %> Changed to a language, ended with a semicolon, and unified with renderaction
Verify controls
Server-side verification,
The effect is as follows:
The usage is as follows:
Model:
Public Class Mymodel
{
Public Int Id { Get ; Set ;}
Public String Name { Get ; Set ;}
}
View:
< H3 > Verify controls </ H3 >
<% Using (html. Form ( " Home " , " Save " , Formmethod. Post )){ %>
<% = Html. Textbox ( " ID " ) %>
<% = Html. validationmessage ( " ID " , New {Style = " Color: Green " }) %>
<% = Html. Textbox ( " Name " ) %>
<% = Html. validationmessage ( " Name " ) %>
<% = Html. submitbutton () %>
<%
} %>
< Div Style = "Color: Red" >
<% = Html. validationsummary () %>
</ Div >
Controller:
Public Class Homecontroller: controller {
Public Actionresult index () {// display the page of the form
Viewdata [ " Title " ] = " Home Page " ;
Viewdata [ " Message " ] = " Welcome to ASP. net mvc! " ;
Return View ();
}
[Acceptverbs ( " Post " )]
Public Actionresult save ( Int ? ID, String Name)
{// Process the form page
If (ID = Null )
Viewdata. modelstate. addmodelerror ( " ID " , Id. tostring (), " ID is required! " );
If ( String . Isnullorempty (name ))
Viewdata. modelstate. addmodelerror ( " Name " , Name, " Name is required! " );
If (Viewdata. modelstate. isvalid) // operations performed after the verification is successful
Return Redirecttoaction ( " Index " );
Return View ( " Index " ); // Else
}
Public Actionresult about (){
Return View ();
}
}
This is what pv5 provides us with a simple verification function.
Antiforgerytoken
This is a verification submission page, similar to viewstate
Write in the form on the submission page
<% = Html. antiforgerytoken () %>
Generate
< Input Name = "_ Mvc_antiforgerytoken" Type = "Hidden" Value = "Fasczn4p + 6hg977mdox4z9pckoy4vlp6whi0rgd + 2l9mbtnggx4gmn36se4kljzf" />
.
Take the preceding action as an example:
[Validateantiforgerytoken]
Public Actionresult save ( Int ? ID, String Name)
Default items added to dropdownlist
To <% = html. dropdownlist ("select", "cityid", new {@ class = "select"}) %>
Leave a string empty by default.