First, the data validation data validation steps
- To add validation-related attribute tags to a model class
- Import validation-related JS files and CSS files on the client
- Using HTML-assisted methods related to validation
- Determine if server-side validation is done on the server side
Common validation Tags
- Required: non-null validation
- Stringlength: Verifying the length of a string
- RegularExpression: Regular Expression validation
- Compare: Compare two field values for equality
- Range: Scope validation
- Remote: Server Authentication (need to write an action in the controller that returns a value of Jsonresult)
- Custom Validation Tags
JS files related to validation
in ASP. 2 sets of validation frameworks are provided, namely the Microsoft Validation Framework and the jquery validation framework. The jquery validation framework is enabled by default.
Note: The order of these JS files cannot be reversed, where the style used for data validation is defined in the Site.css file.
Validation of related HTML helper methods
Note: 1. You must use Html.validationmessage () and html.validationmessagefor () to display the warning message that the validation is invalid;
2. Validated controls (such as text boxes \ drop-down list boxes, etc.) must be generated using the HTML helper method to be valid.
Determine if the model has passed validation on the server side (server-side validation)
If the MVC validation feature is used in the page, the server-side validation must be determined through modelstate.isvalid in the action in the background. Other business logic code can be executed only through server-side validation
key points of use :
- The validated token cannot be plain HTML and must use the appropriate HTML helper method
For example: Html.textboxfor (Model=>model. UserName), the validation is valid;
<input type= "text" id= "UserName" name= "UserName"/>, the validation is invalid;
- Validation message must be displayed using the appropriate Html helper method (Html.validationmessage)
- The validated control must be placed inside an auxiliary method that has html.beginform (cannot be a pure <form> tag).
- Client authentication is unsafe, easy to disable or spoofed, so be sure to determine whether Modelstate.isvalid is true on the server side.
Action vs. View data transfer
In some cases, the view needs to display multiple copies of data (such as modified employee information \ Job List \ department list), but when action returns a view, the view () method can only pass 1 types of object data, so how can other data be passed?
ASP. NET MVC provides us with 3 scenarios: Viewdata\tempdata\viewbag.
Important: There are three properties in the controller and view, and after storing the data in the controller's corresponding properties, you can extract the data in the corresponding properties of the view.
ViewData
ViewData is a dictionary type. Add data to it in the form of a key-value pair.
You first add to ViewData in action, and then you can access ViewData in view to extract the data.
ViewBag
ViewBag is a property of a dynamic type. The ViewBag attribute was added in ASP. NET MVC 3.0.
Dynamic type: Dynamically, that is, you can add a new property to a property at any time by assigning a value directly to it without defining its property in advance.
The following code demonstrates the storage of CLS and Stu two objects into ViewBag, which are placed in properties ClassInfo and Studentinfo respectively.
TempData
The usage of TempData is similar to that of ViewData, both of which are dictionary types. What's the difference between them?
The data stored in ViewData is valid only for the current action of this access. While the data stored in TempData is valid for the current action of this access, It is also valid for other actions on this visit (such as storing data in Action1 to TempData, even after jumping from Action1 to Action2, you can still extract data from Action2 in TempData. and ViewData and ViewBag are unable to do this). Therefore, the life cycle of data in TempData is more than viewdata| The life cycle in the viewbag is long.
Implementation class of ActionResult
In the ASP. NET MVC model, the action responds to various requests from the user, such as returning HTML documents, HTML fragments, JSON data, plain text, files, and so on to the client. The various results above are implemented in ASP. NET MVC by returning an object of type ActionResult in the action (behavior or action) method.
The ActionResult class is an abstract class (abstract classes cannot be instantiated directly).
ActionResult has many implementation classes, and different implementation classes are used to respond to different results from the client. and a number of auxiliary methods are provided in the Controller class, which can quickly create various actionresult.
In actual development, we are more using the helper methods in the Controller class, rather than manually creating the ActionResult implementation class.
Viewresult class
The purpose of the Viewresult class is to respond to a view file in the view directory to the client.
The view () method is provided in the Controller class to quickly return a Viewresult class object.
Contentresult class
This class is used to return a piece of textual content (plain text \html ...) to the client.
The helper methods in the Controller class are: Controller.content (string Content);
Redirecttorouteresult class
The purpose of this class is to jump from the current action to another action.
The helper methods in the Controller class are: Redirecttoaction ("ActionName", "Controllername")
Redirectresult class
Jumps from the current action to a different URL address on the server side.
The helper method in the Controller class is: Controller.redirect (string URL)
Jsonresult class
This class is used to return a JSON-formatted string to the client.
The helper method in the Controller class is: Controller.json (object data);
Note: By default, MVC refuses to respond to Ajax requests that are sent in Get mode, requiring the use of jsonrequestbehavior.allowget.
Partialviewresult class
The purpose of this class is to respond to a partial view file of the views directory to the client. A partial view is a view file that contains only HTML fragments.
The auxiliary methods in the Controller class are: Controller.partialview ();
Demonstrates how to create a partial view file when you create a view file (tick the Create as partial view check box):
Filepathresult class
This class is used to implement file downloads.
The helper methods in the Controller class are: Controller.file (string FilePath, String ContentType, string fileName);
Filecontentresult class
Outputs an in-memory byte array as file data directly to the client. Ideal for dynamically generating files in memory or loading small files into a byte array after output.
Filestreamresult class
This method first reads all the files into the server memory, buffers them, and then sends them to the client. Doing so consumes server memory very much, so you should avoid sending large files using this method.
Emptyresult class
This class is used to respond to a null result to the client.
Httpstatuscoderesult class
This class is used to specify the status code accordingly to the client.
The Httpnotfoundresult class is the subclass of Httpstatuscoderesult, which represents the 404 status code.
The following are some common HTTP status codes:
404: Resource Not Found (indicates that the resource address of the client pull server does not exist)
500: Server Internal error (starting with 5XX indicates server side error during processing)
200: Successful (indicates that the client sent the request was successfully processed and responded by the server)
Other
How do I import namespaces in an ASPX page?
Method One: Add <%@ Import namespace= "namespace" on the specified page%>
Method Two: Add pages---namespaces----in the project's Web. config configuration file
Note: Namespaces added by way two can be used in all ASPX pages, while the namespaces added by way one are only available in the current page.
ASP. NET MVC Step Two