ASP. NET MVC Development Basics Learning notes: Four, checksum, Ajax and filters

Source: Internet
Author: User
Tags jquery library

One, check-form not that you want to mention, you can mention it.

 1.1 dataannotations (data annotations) attributes that are located in the System.ComponentModel.DataAnnotations namespace specify validation for each field in the data model. These attributes are used to define common validation patterns, such as scope checks and required fields. The DataAnnotations feature enables MVC to provide client and server validation checks so that you don't have to do extra coding to control the effectiveness of your data.   by adding dataannotations to the data description for the model class, we can easily add validation capabilities to the application. DataAnnotations allows us to describe the validation rules that you want to apply on model properties, and ASP. NET MVC will use these dataannotations and then return the appropriate validation information to the user.   Among the many built-in verification features DataAnnotations has to offer, four of them are:  (0) [DisplayName]: Display name – The hint name that defines the form field   (1) [Required] : Must – Indicates that this property is a field that must provide content   (2) [stringlength]: String length – The maximum length of a property that defines a string type   (3) [Range]: Range – provides the maximum and minimum values for a property of a numeric type   (4  ) [RegularExpression]: Regular expression – Specifies that data field values in Dynamic Data must match the specified regular expression   1.2 use DataAnnotations to verify the model Suppose we have a UserInfo entity in our model, which is defined as follows:     public class userinfo    {        public int Id {get; set;}         public string UserName {get; set;}         public int age {get; set;}    } The properties of UserInfo are simple, only three: Id,username and age three fields; now IWe can add validation features to it to see the powerful verification capabilities they provide.   (1) Non-null authentication   Add attribute:  copy code [Display (name= "username")][required (errormessage = "* Name required")]public string UserName { Get Set } [Display (Name = "Age")][required (errormessage = "* Ages required")]public int age {get; set;} Copy Code Validation effect:    (2) string length validation   Add attribute:  [display (name= "username")][required (errormessage = "* Name required") [  Stringlength (5, errormessage = "* length must be less than 5")]public string UserName {get; set;} Validation effects:    (3) Scope validation   Add features:  [display (Name = "Age")][required (errormessage = "* Age required")][range (18,  ]public int Age {get; set;} Validation effect:    (4) Regular expression validation   Add attribute: Verifies whether a user enters a number, the regular expression matches  [display (Name = "Age")][required ( ErrorMessage = "* Age required")][range (+)][regularexpression (@ "^\d+$", errormessage = "* Please enter a valid number")]public int ages {get; set ; Validation effect:    (5) Browse generated HTML code     as can be seen, our browser-side checksum is done by setting custom properties for HTML tags, The various check attributes that we add to the model will generate a specific property on the client, for example: data-val-length-max= "5" and data-val-Length= "* length must be less than 5" corresponds to [Stringlength (5, errormessage = "* length must be less than 5")]. Then, with jquery validate, the client checks before each commit, and if there are non-conforming rules in the checksum match, the message is displayed in a specific span label (class= "Field-validation-valid"). and prevent this form from submitting operations.  1.3 precautions for using dataannotations (1) First, make sure that the page that needs to be validated introduces several JS files:  <script src= "@Url. Content (" ~/scripts/ Jquery.validate.min.js ")" Type= "Text/javascript" ></script><script src= "@Url. Content (" ~/scripts/ Jquery.validate.unobtrusive.min.js ")" Type= "Text/javascript" ></script> of course, the JS file of the jquery library is also required, And in the above two JS before the introduction of;  (2) in the Web. config appsettings, has been supported by default client authentication (MVC3.0 and later version of the default support, MVC2.0 need to modify): <!-- Whether to enable global client checksum--><add key= "clientvalidationenabled" value= "true"/><add key= "unobtrusivejavascriptenabled "Value=" true "/>ps:unobtrusive JavaScript has three layers of meaning:  one is not arbitrarily inserted in the HTML code javsscript code, only add some additional property values in the tag, The referenced script file is then identified and processed;  two is a progressive enhancement of the functionality added by the script file, when the client does not support or disables javsscript, the functionality provided by the Web page is still achievable, but the user experience is reduced;  Third, can be compatible with different browsers.   (3) in action if you want to verify that the client has passed a checksum, you can implement the:  by using the following codeCopy code         [httppost]        public ActionResult Add (UserInfo UserInfo)   &NB Sp     {            if (modelstate.isvalid)             {                 //to do fun           }&NBS p;            return redirecttoaction ("Index");       } copy code if verified, MO The IsValid property of the Delstate (bool type) becomes true, and vice versa is false.   Two Ajax methods under ASP.  2.1 using jquery Ajax first, using this approach in ASP. NET MVC is consistent with the way common WebForm is developed, and it's important to note that URL addresses are different- > Requests the action under the Controller, for example, the URL requested in WebForm is usually/ajax/userhandler.ashx, whereas the URL requested in MVC is usually:/user/getall.   For example, we add a button to a view that uses Ajax to get a server-side time:   javascript  <add key= "unobtrusivejavascriptenabled" value= "true"/> (2) Second, use the Ajax.beginform method to construct a form form:  copy code        

ASP. NET MVC Development Basics Learning notes: Four, checksum, Ajax and filters

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.