Sunday 16 May 2010

Attribute Validation

Below "App_Code", under the database file, open the "designer"

Look for a table name called: [Table(Name="dbo.Products")]

Right-click "App_Code", "Add New Item", select "Class" then name it the same as the "designer" table name.

Now change "public class Products" to "public partial class Products"

Delete text below, inbetween brackets - public Products()
{
//
// TODO: Add constructor logic here
//
}


At the top of the page declare -
using System.Web.DynamicData;
using System.ComponentModel.DataAnnotations;

Above: "public partial class Products", enter: [MetadataType (typeof(ProductMetaData))]

At the end of the coding (after the last "}" bracket), enter:public class ProductMetaData
{
}

Between the above brackets enter:[Required]
[Range (0,150, ErrorMessage="Enter value between 0 - 150")]public object UnitsInStock;

Once you have done that run the website by clicking the play button.

The final code should be as follows:

using System;
using System.Collections.Generic;
using System.Linq;using System.Web;
using System.Web.DynamicData;
using System.ComponentModel.DataAnnotations;

///
/// Summary description for Product
///
[MetadataType (typeof(ProductMetaData))]
public partial class Product
{
}
public class ProductMetaData
{
[Required][Range (0,150,ErrorMessage="Insert value between 0 to 150")]
public object UnitsInStock;
}

No comments:

Post a Comment