Dynamically apply validation rules at runtime with ASP.NET MVC 4

前端 未结 4 1812
遇见更好的自我
遇见更好的自我 2020-12-14 13:02

I\'ve been working in WebForms for years but I\'m fairly new to .NET\'s flavor of MVC. I am trying to figure out how to apply dynamic validation rules to members of my model

4条回答
  •  自闭症患者
    2020-12-14 13:16

    I havent been working with MVC4 for a long time, so forgive me if i am wrong, but you can server side and client side validation using jquery-val (already available to you if you used the "internet application" template when creating your project) and attributes:

    public class Device
    {
        public int Id {get; set;}
        public ICollection Settings {get; set;}
    }
    
    public class Setting
    {
        [Required]
        public int Id {get; set;} 
        [Range(1,10)]
        public string Value {get; set;}
        [Required]
        public bool IsRequired {get; set;}
        public int MinLength {get; set;}
        public int MaxLength {get; set;}
    }
    

提交回复
热议问题