Using VS2010 and MVC2.0 to enhance verification capabilities

Source: Internet
Author: User

Before I begin, I have to say that I have installed vs RC1 and used it to convert the old version to ASP.net 4.0

In most cases, when you receive information from a user post from a form form, your validation code often checks to see if the value exists, whether the data type is correct, and whether the data is in the correct range. At the very least, you should make sure that whenever a function accepts user input, it should perform the appropriate validation logic. This means that sometimes in different locations throughout the program, you will validate the same or similar values multiple times. For example, if you want to limit the user's last name to no more than 20 characters, such validation needs to be implemented in several parts of the program. Until the appearance of the Vennegoor of Hesselink (a Dutch team player, currently doomed Hull City FC), he came to your website to buy an earring worth 50,000 pounds before his salary was downgraded with the team he served. He was registered in his 22-character-length surname, but when he found out that his surname was not allowed, he angrily left your website and left a disgruntled comment on the web master. Obviously, you don't want to have a similar situation, so you have to find all the code in your program that verifies the length of the last name and fix it so that you can accept longer names ...

If you put the validation code in a centralized place, would it be easier to change the same as the above? The dataannotations in model is precisely the reason for this, in MVC2.0, this feature is included.

DataAnnotations has been part of the. NET Framework for some time, but MVC2.0 added Modelmetadata classes, which are containers for storing metadata, The default is to use the Dataannotationsmetadataprovider class, which is also the new class. Because the value passed in is taken by the action method to accept model binding as the parameter that matches the incoming parameter and action, in MVC2.0, the default model Binder uses Dataannotationsmetadataprovider to get the object that the model binder in metadata tries to match, and if the validation is metadata exists, it is validated by comparing the properties of the object with the values passed in. This kind of meta is implemented by you by using tags to modify attributes.

In the following example I use the process of adding a contact from the original program to describe the method of using Dataannotatioins, where we use a custom ViewModel named: Contactpersonviewmodel. Add the contact by using the Contact.add () action method as follows:

using System;
using System.Collections.Generic;
using System.Web.Mvc;
using System.ComponentModel;

namespace ContactManagerMVC.Views.ViewModels
{
  public class ContactPersonViewModel
  {
   public int Id { get; set; }
   public string FirstName { get; set; }
   public string MiddleName { get; set; }
   public string LastName { get; set; }
   public DateTime DateOfBirth { get; set; }
   public IEnumerable<SelectListItem> Type { get; set; }
  }
}

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.