asp.net MVC 2 client-side validation extension

Source: Internet
Author: User
Tags net return client

asp.net MVC 2 built-in support for validating data annotation properties on the server, this article describes how to build custom validation properties using the underlying classes in System.ComponentModel.DataAnnotations, about ASP.net MVC 2 How data annotations work, refer to Brad's blog (http://bradwilson.typepad.com/blog/2009/04/dataannotations-and-aspnet-mvc.html).

I'll show you how to connect to the client-side validation extensions for ASP.net MVC 2 so that you can run JavaScript validation logic on the client.

I will create a priceattribute to verify that a value is greater than the specified price and that the price must end at 99, so $20.00 is an invalid value and $19.99 is valid. The following is the code for this property:

The following are the referenced contents:

  1. Public class Priceattribute:validationattribute {
  2. Public Double Minprice { get; set; }
  3. Public Override BOOL IsValid (object value) {
  4. if (value = = null) {
  5. return true;
  6. }
  7. var price = (double) value;
  8. if (Price < Minprice) {
  9. return false;
  10. }
  11. double cents = price-math.truncate (price);
  12. if (Cents < 0.99 cents >= 0.995) {
  13. return false;
  14. }
  15. return true;
  16. }
  17. }

Note If the value is NULL, the value returned is true, and this property does not verify that the field is required. I will verify that the value is required in the RequiredAttribute. It allows me to place the attribute on an optional value, displaying an error when the user leaves the field blank.

We can create a view model, then apply this property to the model for quick testing, and here's the code for the model:

The following are the referenced contents:

    1. Public class Productviewmodel {
    2. [Price (Minprice = 1.99)]
    3. Public Double Price { get; set; }
    4. [Required]
    5. Public string Title { get; set; }
    6. }

We quickly create a view (index.aspx) to display and edit the form:

The following are the referenced contents:

  1. <%@ Page Language="C #" Inherits=" viewpage" %>
  2. <% using (Html.BeginForm ()) {%>
  3. <%= html.textboxfor (m => m.title)%>
  4. <%= html.validationmessagefor (m => m.title)%>
  5. <%= html.textboxfor (m => m.price)%>
  6. <%= html.validationmessagefor (m => m.price)%>
  7. <input type="Submit" />
  8. <%}%>

Now all we need is a controller with two behaviors, one to edit the view, the other to receive the submitted Productviewmodel.

The following are the referenced contents:

  1. [HandleError]
  2. Public class Homecontroller:controller {
  3. Public ActionResult Index () {
  4. return View (new Productviewmodel ());
  5. }
  6. [HttpPost]
  7. Public ActionResult Index (Productviewmodel model) {
  8. return View (model);
  9. }
  10. }

We haven't turned on client-side validation yet, let's see what happens when we look at the 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.