ASP. net mvc data verification and related content, asp. netmvc
I. Data Verification
Data verification steps add verification-related feature tags to the model class import and verification-related JavaScript and css files on the client using verification-related Html auxiliary methods on the server side to determine whether common verification tags verified on the server
Required: non-empty validation StringLength: Verify the length of the string RegularExpression: Regular Expression validation Compare: Compare the values of the two fields are equal Range: Range verification Remote: server verification (the Action with the return value of JsonResult needs to be written in the controller) custom verification mark js file related to verification
ASP. net mvc provides two verification frameworks, Microsoft verification framework and jquery verification framework. The jquery verification framework is enabled by default.
Note: The sequence of these JS files cannot be reversed. The site.css file defines the style used for data verification.
Verification-related Html auxiliary methods
Note: 1. Html. ValidationMessage () and Html. ValidationMessageFor () must be used to display the verification failure prompt information;
2. the verified control (such as the text box or drop-down list box) must be generated using the Html auxiliary method.
Verify the model on the server (server-side verification)
If the MVC verification function is used in the page, you must use ModelState. IsValid in the corresponding Action in the background to determine whether the server-side verification is successful. Other business logic code can be executed only when server-side verification is passed
Usage tips:
The authenticated tag cannot be pure html, and the corresponding html auxiliary method must be used.
For example, Html. TextBoxFor (model => model. UserName) is valid;
<Input type = "text" id = "UserName" name = "UserName"/>, the verification is invalid;
The corresponding html auxiliary method must be used to display the verification message (Html. validationMessage) the verified control must be placed in html. the auxiliary method of beginform is internal (cannot be a pure <form> flag ). client Authentication is insecure and prone to being disabled or spoofed. Therefore, you must determine ModelState on the server. whether IsValid is true.
Data transmission between Action and View
In some cases, the View needs to display multiple copies of data (such as modified employee information, job list, and department list), but when the Action returns to the View, View () the method can only pass data of the object type. How to transfer other data?
ASP. net mvc provides three solutions: ViewData \ TempData \ ViewBag.
Important: The three attributes are available in the Controller and View. After the data is stored in the corresponding attributes of the Controller, the data can be extracted from the corresponding attributes of the View.
ViewData
ViewData is a dictionary type. When adding data to it, you must add it as a key-value pair.
Add to ViewData in Action first, and then access ViewData in View to extract data.
ViewBag
ViewBag is a dynamic attribute. The ViewBag attribute is added only in ASP. net mvc 3.0.
Dynamic type: dynamic can add new attributes at any time without defining attributes for it.
The following code demonstrates storing two objects cls and stu in ViewBag, which are put in the attributes ClassInfo and StudentInfo respectively.
TempData
The usage of TempData is very similar to that of ViewData. They are all dictionary types. What are their differences?
The data stored in ViewData is only valid for the current action of this access. the data stored in TempData is not only valid for the current action of this access, but also for other actions of this access (for example, data is stored in TempData in Action1, even after redirecting from Action1 to Action2, you can still extract data from TempData in action2. ViewData and ViewBag cannot do this). Therefore, the data lifecycle in TempData is longer than that in ViewData | ViewBag.
Implementation class of ActionResult
In the ASP. net mvc model, Action is used to respond to various user requests, such as returning html documents, html fragments, json data, plain text, and files to the client. In ASP. net mvc, The ActionResult type object is returned in the Action (Action or operation) method to respond to the above results to the client.
The ActionResult class is an abstract class (the abstract class cannot be directly instantiated ).
There are many implementation classes for ActionResult. Different implementation classes are used to respond to different client results. A large number of auxiliary methods are provided in the Controller class. These auxiliary methods can quickly create various actionresults.
In actual development, we use the auxiliary methods in the Controller class instead of manually creating the implementation class of ActionResult.
ViewResult class
The ViewResult Class responds to a View file in the View directory to the client.
The Controller class provides the View () method to quickly return a ViewResult class object.
ContentResult class
This class is used to return a piece of text (plain text \ HTML...) to the client ...).
The auxiliary method in the Controller class is Controller. Content (string content );
RedirectToRouteResult class
This class redirects from the current action to other actions.
The auxiliary method in the Controller class is RedirectToAction ("ActionName", "ControllerName ")
RedirectResult class
Jump from the current Action to another URL on the server side.
The auxiliary method in the Controller class is Controller. Redirect (string url)
JsonResult class
This class is used to return a Json string to the client.
The auxiliary method in the Controller class is Controller. Json (object data );
Note: by default, mvc rejects ajax requests sent in get mode, and JsonRequestBehavior. AllowGet is required.
PartialViewResult class
This class responds to a partial view file in the Views directory to the client. A partial view is a view file that contains only html fragments.
The auxiliary method in the Controller class is Controller. PartialView ();
Demonstrate how to create a partial view file when creating a View File (check the "Create as partial view" check box ):
FilePathResult class
This class is used to download files.
The auxiliary methods in the Controller class are: Controller. File (string filePath, string contentType, string fileName );
FileContentResult class
The byte array in the memory is directly output to the client as file data. It is suitable for dynamically generating files in the memory or loading small files to byte Arrays for output.
FileStreamResult class
This method will first read all the files into the server memory, buffer them, and then send them to the client. This will consume a lot of server memory, so avoid using this method to send large files.
EmptyResult class
This class is used to return an empty result to the client.
HttpStatusCodeResult class
This class is used to specify the status code to the client.
The HttpNotFoundResult class is a subclass of HttpStatusCodeResult, indicating the status code 404.
Below are some common http status codes:
404: the resource is not found (indicating that the resource address of the client request server does not exist)
500: internal server error (an error starting with 5XX indicates that the server encountered during processing)
200: Successful (indicating that the request sent by the client is successfully processed and responded by the server)
Others
On the aspx page, how does one import a namespace?
Method 1: Add <% @ Import Namespace = "Namespace" %> on the specified page
Method 2: add pages --- namespaces ---- add to the project's web. config configuration file.
Note: The namespace added through method 2 can be used on all aspx pages, while the namespace added by method 1 can only be used on the current page.
Aspnet mvc data type verification
RegularExpression should be used
[RegularExpression (@ "^ \ d + $", ErrorMessage = "must be a number")]
Is aspnet mvc data annotation a server verification or dual verification?
Server verification is essential-it truly guarantees data security.
There is only one application for client verification. It increases the experience of the customer!
For example, you only need to enter numbers in a text box. You can use JS for client verification. If the input is incorrect, the system prompts the customer and increases the user experience.
However, You don't guarantee that JavaScript works at any time. Don't use it. It's easy for people who do not work with JavaScript, or upload other illegal data-so if you want to avoid mistakes, server verification is essential.
One sentence in the design is very important-do not trust client data at any time.