Detailed description of ASP. net mvc 2 custom Verification

Source: Internet
Author: User

BKJIA exclusive Article] ASP. net mvc 2 has built-in support for verifying data annotation verification properties on the server. This article describes how to use System. componentModel. the basic class in DataAnnotations builds custom validation attributes, about ASP. net mvc 2 Data annotation is how to work, please refer to Brad's blog http://bradwilson.typepad.com/blog/2009/04/dataannotations-and-aspnet-mvc.html ).

  • Microsoft released multiple functions of ASP. net mvc 2 preview Edition
  • Scott Gu teaches you how to use ASP. net mvc 2 new features
  • Hot weekly development: ASP. net mvc 2 released STM Tao
  • Hot weekly development: ASP. NE, Windows 7 Code Competition
  • Detailed introduction of ASP. net mvc for general addition, deletion, and modification of tables

I will introduce how to connect to the client verification extension of ASP. NET MVC 2 so that you can run JavaScript verification logic on the client.

I will create a PriceAttribute to verify whether a value is greater than the specified price and the price must end at 99, so $20.00 is invalid and $19.99 is valid. The code for this attribute is as follows:

 
 
  1. public class PriceAttribute : ValidationAttribute {  
  2.   public double MinPrice { getset; }  
  3.       
  4.   public override bool IsValid(object value) {  
  5.     if (value == null) {  
  6.       return true;  
  7.     }  
  8.     var price = (double)value;  
  9.     if (price < MinPrice) {  
  10.       return false;  
  11.     }  
  12.     double cents = price - Math.Truncate(price);  
  13.     if(cents < 0.99 || cents >= 0.995) {  
  14.       return false;  
  15.     }  
  16.          
  17.     return true;  
  18.   }  

Note: If the value is null, the returned value is true. This attribute does not verify whether the field is required. I will verify whether the value is required in RequiredAttribute. It allows me to place attributes on optional values. An error is displayed when you leave this field blank.

We can create a view model and apply this attribute to the model for rapid testing. The code for this model is as follows:

 
 
  1. public class ProductViewModel {  
  2.   [Price(MinPrice = 1.99)]  
  3.   public double Price { getset; }  
  4.  
  5.   [Required]  
  6.   public string Title { getset; }  

Create a View Index. aspx) to display and edit the form:

 
 
  1. <%@ Page Language="C#" Inherits="ViewPage "  %> 
  2.  
  3. <% using (Html.BeginForm()) { %> 
  4.  
  5.   <%= Html.TextBoxFor(m => m.Title) %> 
  6.     <%= Html.ValidationMessageFor(m => m.Title) %> 
  7.   <%= Html.TextBoxFor(m => m.Price) %> 
  8.     <%= Html.ValidationMessageFor(m => m.Price) %> 
  9.       
  10.     <input type="submit" /> 
  11. <% } %> 

Now we only need one controller with two actions, one editing view, and the other receiving the submitted ProductViewModel.

 
 
  1. [HandleError]  
  2. public class HomeController : Controller {  
  3.   public ActionResult Index() {  
  4.     return View(new ProductViewModel());  
  5.   }  
  6.  
  7.   [HttpPost]  
  8.   public ActionResult Index(ProductViewModel model) {  
  9.     return View(model);  
  10.   }  

We have not enabled client verification. Next let's see what happens when we view this page and submit some values.


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.