asp.net MVC model validation based on annotation features: a model, multiple validation rules

Source: Internet
Author: User
Tags definition visual studio

For model verification, the ideal design should be scene driven, not model (type) driven, that is, for the same model object, in different use scenarios may have different validation rules. To give a simple example, for a candidate to represent the data object, for the position of the candidates, certainly for the applicant's age, gender, professional skills and other aspects have different requirements. But the model validation of ASP.net MVC is model driven, because validation rules are applied to model types and their properties in the form of validation attributes. Such validation actually limits the reuse of model types in usage scenarios based on different validation rules. Through the extension of the previous article "Applying Validationattribute to Parameters", we have applied the validation attribute directly to the parameter, which solves the problem to some extent, but only solves some problems. Because the validation attribute applied to the parameter can only be used for validation at the parameter type level, it cannot be used for validation on the attribute level of the parameter type (the source code is downloaded from here).

One, the same model in the use of different validation rules

Now we implement a model validation based on different validation rules by leveraging the extension of asp.net mvc. To give the reader a sense of the identity of this authentication, let's take a look at how this extension ultimately implements the validation effect. In an empty Web application created through the ASP.net MVC project template for Visual Studio, we defined a person type as model.

   1:public class Person
2: {
3: [DisplayName ("name")]
4: Public string Name {get; set;}
5:
6: [DisplayName ("gender")]
7: Public string Gender {get; set;}
8:
9: [DisplayName ("Age")]
[RangeValidator (rulename = "Rule1", errormessage = "{0}" must be between {1} and {2}!) ")]
One: [RangeValidator, RuleName = "Rule2", errormessage = "{0} must be between {1} and {2}!" ")]
[RangeValidator, RuleName = "Rule3", errormessage = "{0} must be between {1} and {2}!" ")]
Public int Age {get; set;}
14:}

Three rangevalidatorattribute (not rangeattribute) are applied on the age attribute that represents ages, and they correspond to three different validation rules for age, and the RuleName attribute represents the rule name. The three validation rules (Rule1, Rule2, and Rule3) require ages of 10 to 20, 20 to 30 and 30-40 respectively.

We then define HomeController with the following definition, which has three Group of action methods (Index, Rule1, and Rule2). Methods a Validationruleattribute attribute is applied on the Rule1, Rule2, and HomeController classes to specify the validation rules currently in use. The Validationruleattribute attribute used to specify the validation rule can be applied to both the controller type and the action method, and the Validationruleattribute attribute to the latter has a higher priority. For the definition of HomeController, the validation rules used by the action method index, Rule1, and Rule2 are Rule3, Rule1, and Rule2 respectively.

1: [ValidationRule ("Rule3")]
2:public class Homecontroller:rulebasedcontroller
3: {
4:public ActionResult Index ()
5: {
6:return View (' person ', new person ());
7:}
8: [HttpPost]
9:public actionresult Index (person person)
10: {
11:return View ("person", person);
12:}
13:
[ValidationRule ("Rule1")]
15:public ActionResult Rule1 ()
16: {
17:return View (' person ', new person ());
18:}
: [HttpPost]
[ValidationRule ("Rule1")]
21:public actionresult Rule1 (person)
22: {
23:return View ("person", person);
24:}
25:
Num: [ValidationRule ("Rule2")]
27:public ActionResult Rule2 ()
28: {
29:return View (' person ', new person ());
30:}
: [HttpPost]
: [ValidationRule ("Rule2")]
33:public actionresult Rule2 (person)
34: {
35:return View ("person", person);
36:}
37:}

The 6 methods defined in HomeController render the created/received person object to the view named person, as shown in the following definition. This is a strongly typed view of the person type as model, where we will render the person object of model in a form in edit mode and provide a Submit button in the form.

   1: @model person
2: @using (Html.BeginForm ())
3: {
4: @Html. Editorformodel ()
5: <input type= "Submit" value= "Save"/>
6:}

Now run our program and access the three action (Index, Rule1, and Rule2) defined in HomeController by specifying the corresponding address in the browser, and a form for editing personal information will appear. Then we enter an illegal age according to the validation rules adopted by the three action methods, then click on the "Save" button, we will see the age of the input according to the corresponding rules are validated, the specific validation effect shown in the following figure.

Related Article

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.