Thursday, April 3, 2014

Uses Of Data Annotation Attributes To Control The Behavior of The Model Classes

Uses of Data Annotation Attributes 

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

namespace DhakaUniversity.Models
{
    public class Student
    {
        public int ID { get; set; }
         [Required]
         [StringLength(50)]
         [Display(Name = "Last Name")]
        public string LastName { get; set; }
         [Required]
         [StringLength(50, MinimumLength = 1, ErrorMessage = "First name cannot be longer than 50  characters.")]
         [Column("FirstName")]
         [Display(Name = "First Name")]
        public string FirstMidName { get; set; }
         [DataType(DataType.Date)]
         [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = True )]
         [Display(Name = "Enrollment Date")]
        public DateTime EnrollmentDate { get; set; }
         [Display(Name = "Full Name")]
        public string FullName
        {
            get
            {
                return LastName + ", " + FirstMidName;
            }
        }
        //A course can have any number of students enrolled in it, so the Enrollments navigation property is a collection:
        public virtual ICollection<Enrollment> Enrollments { get; set; }
    }
}