In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article focuses on "how to achieve ASP.NET MVC5 add verification", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor to take you to learn "how to achieve ASP.NET MVC5 add verification"!
1. Add validation to the Model class as follows:
Public class Movie {public int ID {get; set;} [StringLength (60Magnem MinimumLengthreading 3) ErrorMessage= "the length of the topic must be 3 to 60 characters")] public string Title {get; set;} [Display (Name= "ReleaseDate")] [DataType (DataType.Date)] [DisplayFormat (DataFormatString= "{0:yyyy-MM-dd}", ApplyFormatInEditMode=true)] public DateTime ReleaseDate {get; set } [Regular_Expression (@ "^ [Amurz] + [Amurz] * $")] [Required] [StringLength (30)] public string Genre {get; set;} [Range (1100)] [DataType (DataType.Currency)] public decimal Price {get; set;} [Regular_Expression (@ "^ [Amurz] + [Amurz'] * $")] public string Rating {get; set;}
This StringLength property, which sets the maximum string length, limits the database, so the database structure changes. Let's first take a look at the definition before the database structure has not changed.
two。 Please look at the picture:
3. In the picture above, you can see that all fields of string type are NVARCHAR (MAX). We will use database migration technology to update the table structure, open the package manager console and enter the following instructions:
Add-migration DataAnnotations
Update-database
When this directive ends, VS opens the generated migration file, and in the Up method, you see the added database constraints:
4. Please look at the picture:
5. Next, let's open the database and see what happens to the structure of the data table:
6.
This validation property specifies the properties you want to apply to the Model. The Required and MinimumLength attributes indicate that there must be a value, but it does not prevent the user from entering a space to satisfy this validation. The RegularExpression attribute is used to restrict what characters can be entered. In the above code, Genre and Rating fields must only be letters. (spaces, numbers, and other special characters are not allowed. The Range property constrains that a value must be within a certain range, and the StringLength property allows you to set the maximum length of the string and the value type (for example, decilmal,int float,DateTime) are inherently required, but not for Required.
Code First ensures that the validation you specify in Model can be validated before the database data is saved. For example, the following code, when called by SaveChanges, will throw an error because some of the necessary fields are missing.
MovieDBContext db = new MovieDBContext (); Movie movie = new Movie (); movie.Title = "Gone with the Wind"; db.Movies.Add (movie); db.SaveChanges (); / /
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.