ASP. NET MVC Model
Validation
(
a
)
Preface
The first part of the model binding is described in general, from the beginning of this chapter into the Model validation section, this is actually a series of model binding is often accompanied by validation. will also explain in the later space the MVC framework Model validation mechanism, as well as some model validation of the way to explain, this chapter is just a simple example of space, for the basic friends can skip directly (can not delay everyone time).
Model
Validation
Model validation Simple Application Example
Modelvalidator using the build process
Custom Implementation Defaultmodelbinder to verify
Custom Modelvalidatorprovider and Modelvalidator
Validationattribute attribute classes use
Custom Validationattribute An example implementation of an attribute class
Model Validation Simple Application Example
ViewModel the definition
First look at the definition of ViewModel, model verification is of course inseparable from model, example code 1-1.
Code 1-1
namespace mvcapplication.models{//<summary>//viewmodel-User registration information///</summary> public class Re gistrationinformation {public string ID {get; set;} public string UserID {get; set;} public string Password1 {get; set;} public string Password2 {get; set;} public string Name {get; set;} }}
In code 1-1, the registrationinformation type is defined to contain some of the properties of the registration information.
Configuring the Environment
With ViewModel, we need a data presentation page, we define a Modelvalidatorcontroller controller, and in which we define the index () method, which displays the information that the page provides to the user for registration. Example code 1-2.
Code 1-2
Namespace mvcapplication.controllers{public class Modelvalidatorcontroller:controller {public Actionresul T Index () {return View (new Models.registrationinformation ()); } }}
Then we'll define the code for the page, which is the index view of the index () method, example code 1-4.
Code 1-3
@model mvcapplication.models.registrationinformation@{viewbag.title = "Index";}
The form in code 1-3 defines the Modelvalidator () method that points to the Modelvalidatorcontroller controller, and I'm sorry I didn't separate the named well-defined areas.
Now that code 1-3 points to the Modelvalidator () method, we need to modify the code in sample code 1-2 to add sample code 1-4 to sample code 1-2.
Code 1-4
public actionresult modelvalidator ( Registrationinformation reginfo) { if (String. IsNullOrEmpty (reginfo.id) | | regInfo.ID == "") { this. Modelstate.addmodelerror ("ID", "Please enter Id,id cannot be empty!"); } if (String. IsNullOrEmpty (Reginfo.userid) | | regInfo.UserID == "") { this. Modelstate.addmodelerror ("UserID", "Please enter user account, user account cannot be empty!" "); } if (String. IsNullOrEmpty (reginfo.password1) | | regInfo.Password1 == "") { this. Modelstate.addmodelerror ("Password1", "Please enter the login password, login password can not be empty!"); } if (String. IsNullOrEmpty (reginfo.password2) | | regInfo.Password2 == "") { this. Modelstate.addmodelerror ("Pssword2", "Please enter the password again, the password cannot be empty!"); } if (!string. IsNullOrEmpty (REGINFO.PASSWORD2)) { if ( REGINFO.PASSWORD2&NBSP;!=&NBSP;REGINFO.PASSWORD1) { this. Modelstate.addmodelerror ("Password2", "Please re-enter the password, unlike the last password"); } } return view ( Reginfo); }
In code 1-4 is to verify the model, you can see that is a simple validation, the judge cannot be empty, the input of the password to the same two.
1-4 is already the model binding, the system has been implemented into the Controller method inside, and in the model validation, and the real validation is in the model binding phase, this part of the content will be explained in the next chapter.
Let's take a look at the view definition for the Modelvalidator () method, code 1-5.
Code 1-5
@model mvcapplication.models.registrationinformation@{viewbag.title = "Modelvalidator";}
In code 1-5, the Html.validationsummary () method is called, and the purpose of this view helper is to generate HTML code for the exception information generated by our model validation.
Now let's look at the project run 1
Figure 1
650) this.width=650; "src=" Http://images.cnitblog.com/i/627988/201407/082014387078616.png "width=" 640 "height=" 264 "/>
According to the validation in the Modelvalidator () method, we deliberately entered this, look at the click “ Submit ” later display to the Modelvalidator page, Figure 2
Figure 2
650) this.width=650; "src=" Http://images.cnitblog.com/i/627988/201407/082015087702567.png "width=" 646 "height=" 293 "/>
Some friends may say that the display of the non-common sense, where there is a hint of the wrong information displayed in the above, can be modified, modify code 1-5, example code 1-6.
Code 1-6
@model mvcapplication.models.registrationinformation@{viewbag.title = "Modelvalidator";}
After the modification we still use the previous test data to see the final result Figure 3.
Figure 3
650) this.width=650; "src=" Http://images.cnitblog.com/i/627988/201407/082015511297841.png "width=" 635 "height=" 235 "/>
A simple demonstration is here, and the next one explains where and how the model verification is located in the MVC framework and the approximate process.
This article is from the "Jinyuan" blog, be sure to keep this source http://jinyuan.blog.51cto.com/8854733/1436045