Sunday, January 4, 2015

Metadata Class for Data Validation in asp.net MVC Database First

For validate data to your database first model, add a class Metadata.cs to your Models folder.
Now write code in metadata class like this:
1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;


namespace POSMSWeb.Models
{
    public class CustomerMetadata
    {
        [Display(Name = "Client Name")]
        public string CustomerName;

        [Display(Name = "Client Address")]
        public string CustomerAddress;

        [Display(Name = "Phone Number")]
        public string CustomerPhone;

        [Display(Name = "Mobile Number")]
        public string CustomerMobile;

        [Display(Name = "FAX Number")]
        public string CustomerFax;

        [Display(Name = "Client Email")]
        public string CustomerEmail;

        [Display(Name = "Client Note")]
        public string CustomerNote;
    }
}


Now, you must associate the model classes with the metadata classes.

In the Models folder, add a class named PartialClasses.cs

Replace the contents of the file with the following code:

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace POSMSWeb.Models
{
    [MetadataType(typeof(CustomerMetadata))]
    public partial class tbl_Customer
    {

    }
}


No comments:

Post a Comment