Add Model Verification
This section adds the verification logic for the movie model and ensures that the verification rules are always executed effectively when you try to create or edit a movie.
First, we need to add a reference to the "system. componentmodel. dataannotations" namespace, which is a namespace in. NET Framework. It provides many built-in verification rules that you can explicitly specify for any class or attribute.
Because our model class movie is automatically generated by entitiy framework, it is not suitable for manual editing. This can easily cause project compilation errors, however, we add verification rules on the basis of the model, which seems to be a dilemma. Let's first look at what the movie provided by Entity Framework to us is like:
We can see that movie is a partial class! This means that we can use the same method to define another partial class, and then extend and add the verification logic for this category .. . NET will compile the classes with the same class names as multiple partial identifiers into one class at runtime, but make sure that all parts are classified under the same namespace!
Create a class movievalidation. CS in the Dal folder and write the following code:
using System;using System.ComponentModel.DataAnnotations;namespace MvcMovie.DAL{ [MetadataType(typeof(MovieValidation))] public partial class MOVIE { public MOVIE() { } public class MovieValidation { [Required(ErrorMessage = "Title is required")] public string TITLE { get; set; } [Required(ErrorMessage = "Date is required")] public DateTime RELEASEDATE { get; set; } [Required(ErrorMessage = "Genre must be specified")] public string GENRE { get; set; } [Required(ErrorMessage = "Price Required")] [Range(1, 100, ErrorMessage = "Price must be between $1 and $100")] public decimal PRICE { get; set; } } }}
Save the document, run the program again, and navigate
Http: // localhost: xxx/movies/create.
If you do not enter anything, click "CREATE". Then you can see that the verification rules we have set have taken effect.
We can see that Visual Studio uses the default CSS style to display an error message for us. These error messages are previously set on the model attributes, and they are also verified to be executed simultaneously on the client and server, even if JS scripts are disabled in our browser, we also have server-side verification, which means that the verification rules we set on model attributes can be used multiple times in multiple places. This is exactly the specific implementation of dry (don't repeat yourself. We can view the webpage source file:
<Div class = "editor-field"> <input class = "text-box single-line" data-val = "true" data-Val-number = "the field price must be A number "data-Val-range =" price must be between $1 and $100 "data-Val-range-max =" 100 "data-Val-range-min =" 1 "data-Val-required =" price required "id =" price "name =" price "type =" text "value =" "/> <SPAN class =" field- validation-valid "data-valmsg-for =" price "data-valmsg-replace =" true "> </span> </div>
We can see that MVC automatically generates a verification script for us. Among them, "data-Val" and "data-Val-number" are jquery. validate. unobtrusive. min. JS syntax. This JS file is a JS library encapsulated by Microsoft.
Another important benefit is that you do not need to modify the moviescontroller class or any line of code in create. cshtml to implement data verification at the UI Layer.
For more information about how this verification process is implemented, click here.
Add display format for the movie Model
Sometimes we need to format the display of model attributes and the data type,System.ComponentModel.DataAnnotations
The namespace supports formatting. The following describes the specific operations.
Modify the movievalidation. CS class we created earlier. The display formats of releasedate and price are as follows:
using System;using System.ComponentModel.DataAnnotations;namespace MvcMovie.DAL{ [MetadataType(typeof(MovieValidation))] public partial class MOVIE { public MOVIE() { } public class MovieValidation { [Required(ErrorMessage = "Title is required")] public string TITLE { get; set; } [Required(ErrorMessage = "Date is required")] [DisplayFormat(DataFormatString = "{0:d}")] public DateTime RELEASEDATE { get; set; } [Required(ErrorMessage = "Genre must be specified")] public string GENRE { get; set; } [Required(ErrorMessage = "Price Required")] [DisplayFormat(DataFormatString = "{0:c}")] [Range(1, 100, ErrorMessage = "Price must be between $1 and $100")] public decimal PRICE { get; set; } } }}
We can see that the data of the two fields in the figure has been formatted and displayed.
So far, we have completed an Asp.net mvc3 entry-level instance. For more information, visit:
ASP. net mvc official website tutorial: http://www.asp.net/mvc/tutorials
ASP. net mvc 3 Quick Start:
Http://blog.csdn.net/qwlovedzm/article/details/6313487