@manhng

Welcome to my blog!

Using Data Annotations for Model Validation

August 11, 2017 23:50
public class User
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
    public string Password { get; set; }
    public DateTime CreatedOn { get; set; } = DateTime.UtcNow;
}

public class UserViewModel
{
    [Required]
    public string Name { get; set; }
    [Required, DataType(DataType.EmailAddress)]
    public string Email { get; set; }
    [Required, DataType(DataType.Password)]
    public string Password { get; set; }
    [Required, DataType(DataType.Password), Compare("Password")]
    public string ConfirmPassword { get; set; }
    [Required]
    public bool AgreedToTerms { get; set; }
}


public class Product
{
[StringLength(50),Required]
public object Name { get; set; }
[StringLength(15)]
public object Color { get; set; }
[Range(0, 9999)]
public object Weight { get; set; }
}

ASP.NET MVC Model Binding and Data Annotation

UIHint

The following examples are annotation for common display UI hint.

[Display(Name = "Student ID")]
public int ID { get; set; }

[DisplayFormat(DataFormatString = "{0:d}")]
public DateTime myDate { get; set; }

[UIHint("CustomDateTime")]        // use template to display date
public DateTime updateDate { get; set; }

UIHint
C# – Tạo và sử dụng Custom Attribute

Validation and Model Binding

http://www.dotnettricks.com/learn/mvc/mvc-data-annotations-for-model-validation

http://www.c-sharpcorner.com/article/data-annotations-and-validation-in-mvc/

http://www.c-sharpcorner.com/UploadFile/4b0136/perform-data-annotation-in-Asp-Net-mvc-5/

https://stackoverflow.com/questions/32532610/mvc-data-annotations-in-razor

if (ModelState.IsValid)
{

    //TODO: 
}

Validation with the Data Annotation Validators (C#)

public class Product
{
    public int Id { get; set; }

    [Required]
    [StringLength(10)]
    public string Name { get; set; }

    [Required]
    public string Description { get; set; }

    [DisplayName("Price")]
    [Required]
    [RegularExpression(@"^\$?\d+(\.(\d{2}))?$")]
    public decimal UnitPrice { get; set; }
}

Part 6: Using Data Annotations for Model Validation

https://docs.microsoft.com/en-us/aspnet/mvc/overview/older-versions/mvc-music-store/mvc-music-store-part-6

public virtual Genre Genre { get; set; }
public virtual Artist Artist { get; set; }

Entity Framework Code First Data Annotations

https://msdn.microsoft.com/en-us/library/jj591583(v=vs.113).aspx
    public class Blog
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public string BloggerName { get; set;}
        public virtual ICollection<Post> Posts { get; set; }
    }

    public class Post
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public DateTime DateCreated { get; set; }
        public string Content { get; set; }
        public int BlogId { get; set; }
        public ICollection<Comment> Comments { get; set; }
    }
 
    public class Blog
    {
        [Key]
        public int PrimaryTrackingKey { get; set; }
        public string Title { get; set; }
        public string BloggerName { get; set;}
        public virtual ICollection<Post> Posts { get; set; }
    }

Composite keys:

public class Passport { [Key] public int PassportNumber { get; set; } [Key] public string IssuingCountry { get; set; } public DateTime Issued { get; set; } public DateTime Expires { get; set; } }
    public class Passport
    {
        [Key]
        [Column(Order=1)]
        public int PassportNumber { get; set; }
        [Key]
        [Column(Order = 2)]
        public string IssuingCountry { get; set; }
        public DateTime Issued { get; set; }
        public DateTime Expires { get; set; }
    }
    public class PassportStamp
    {
        [Key]
        public int StampId { get; set; }
        public DateTime Stamped { get; set; }
        public string StampingCountry { get; set; }

        [ForeignKey("Passport")]
        [Column(Order = 1)]
        public int PassportNumber { get; set; }

        [ForeignKey("Passport")]
        [Column(Order = 2)]
        public string IssuingCountry { get; set; }

        public Passport Passport { get; set; }
    }
 

Categories

Recent posts