In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces "how to understand ASP.NET MVC data validation and related content" related knowledge, in the actual case of the operation process, many people will encounter such a dilemma, then let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!
I. Data validation
Steps for data validation Add validation related feature tags to the model class Import validation related js files and css files on the client side Use validation related Html auxiliary methods to determine whether to validate common validation tags on the server side
Required: Non-null validation StringLength: Validates the length of a string RegularExpression: Regular expression validation Compare: Compares the values of two fields for equality Range: Range validation Remote: Server validation (requires writing an Action in controller that returns a value of JsonResult) Custom validation tags Validation related js files
ASP.NET MVC provides two validation frameworks, Microsoft Validation Framework and jquery Validation Framework. The jquery validation framework is enabled by default.
Note: The order of these js files cannot be reversed; the Site.css file defines the style used for data validation.
Verify related Html helper methods
Note: 1. Html.ValidationMessage() and Html.ValidationMessageFor() must be used to display the prompt message of validation failure;
2. Validated controls (e.g. Text Box\Drop Down List Box, etc.) must be generated using Html helper methods to be valid.
Determine whether the model has passed validation on the server side (server-side validation)
If MVC validation is used in the page, then ModelState.IsValid must be used in the corresponding Action in the background to determine whether the server-side validation has passed. Only after server-side validation can other business logic codes be executed.
Key points of use:
The tag being validated cannot be pure html, the corresponding html helper method must be used
For example:Html.TextBoxFor(model=>model.UserName), validation is valid;
, the validation is invalid;
Validation message must be displayed using the appropriate html helper method (Html.ValidationMessage) The validated control must be placed inside a helper method with html. begin (cannot be a pure tag). Client-side validation is insecure and easily disabled or spoofed, so be sure to determine whether ModelState.IsValid is true on the server side.
Data Transfer Between Action and View
In some cases, the view needs to display multiple pieces of data (such as modified employee information\job list\department list), but when Action returns to the view, the View() method can only pass one object type of data, so how should the other data be passed?
ASP.NET MVC provides us with three scenarios:ViewData\TempData\ViewBag.
Important: There are these three attributes in both Controller and View. After storing data in the corresponding attributes of Controller, you can extract these data in the corresponding attributes of View.
ViewData
ViewData is a dictionary type. Add data to it as key-value pairs.
First add to ViewData in Action, then access ViewData in View to extract data.
ViewBag
ViewBag is a dynamically typed property. The ViewBag attribute was added in ASP.NET MVC 3.0.
Dynamic type:dynamic, that is, you can add new attributes to it at any time by directly assigning attributes to it without defining attributes in advance.
The following code demonstrates placing cls and stu objects in ViewBag, which are placed in the properties ClassInfo and StudentInfo, respectively.
TempData
The usage of TempData is similar to ViewData, both are dictionary types. What is the difference between them?
The data stored in ViewData is valid only for the current action accessed. The data stored in TempData is valid not only for the current action of this access, but also for other actions of this access (for example, data is stored in TempData in Action1, and even after jumping from Action1 to Action2, data can still be extracted from TempData in Action2. Neither ViewData nor ViewBag can do that.) Therefore, the life cycle of data in TempData is longer than ViewData.| The life cycle in ViewBag is longer.
ActionResult implementation class
In ASP.NET MVC model, Action is used to respond to various user requests, such as returning html documents, html fragments, json data, plain text, files and other results to the client. Responding to the above various results to the client is accomplished in ASP.NET MVC by returning an ActionResult type object in an Action method.
ActionResult 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 in the Controller class provides a large number of auxiliary methods, these auxiliary methods can quickly create a variety of ActionResults.
In actual development, we are more likely to use auxiliary methods in the Controller class than to manually create the ActionResult implementation class ourselves.
ViewResult class
The ViewResult class responds to a client with a view file in the View directory.
The Controller class provides a View() method to quickly return a ViewResult object.
ContentResult class
This class is used to return a piece of text content (plain text\HTML...) to the client.
Auxiliary methods in the Controller class are:Controller.Content(string content);
RedirectToRouteResult class
The function of this class is to jump from the current action to another action.
Auxiliary methods in the Controller class are:RedirectToAction("ActionName", "ControlName")
RedirectResult class
Jump from the current Action to another URL on the server side.
Auxiliary methods in the Controller class are:Controller.Redirect(string url)
JsonResult class
This class is used to return a string in Json format to the client.
The helper methods in the Controller class are:Controller.Json(object data);
Note: By default, mvc refuses to respond to ajax requests sent as get, which requires JsonRequestBehavior.AllowGet.
PartialViewResult class
This class responds to clients with a partial view file of the Views directory. A partial view is a view file that contains only html fragments.
The auxiliary methods in the Controller class are:Controller.PartialView();
The following illustration demonstrates how to create a partial view file (check the "Create as partial view" checkbox) when creating a view file:
FilePathResult class
This class is used to download files.
Controller.File(string filePath, string contentType, string fileName);
FileContentResult class
Output byte arrays in memory directly to the client as file data. It is suitable for dynamically generating files in memory or loading small files into byte arrays and outputting them.
FileStreamResult class
This method reads all files into server memory, buffers them, and sends them to the client. Doing so consumes a lot of server memory, so 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 assign status codes to clients accordingly.
The HttpNotFoundResult class is a subclass of HttpStatusCodeResult, representing the status code 404.
Here are some common http status codes:
404: Resource not found (indicates that the resource address requested by the client does not exist)
500: Server internal error (errors starting with 5XX indicate server-side errors during processing)
200: Success (indicates that the request sent by the client was successfully processed and responded to by the server)
other
How do I import namespaces into aspx pages?
Method 1: Add to the specified page
Method 2: Add pages---namespaces---add to the project's web.config configuration file
"How to understand ASP.NET MVC data validation and related content" content is introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.