代码之家  ›  专栏  ›  技术社区  ›  William

模型中的MVC验证

  •  0
  • William  · 技术社区  · 14 年前

    我目前正在使用DataAnnotations验证我的MVC 2应用程序。不过,我遇到了一个小问题。

    我现在有一个用户类型的对象,它有许多属性。所有这些都是必需的。

    public class User
        {
    
            [Required(ErrorMessage = "Username is required")]
            public string Username { get; set; }
    
            [Required(ErrorMessage = "Password is required")]
            public string Password { get; set; }
    
            [Required(ErrorMessage = "Email is required")]
            public string Email { get; set; }
    
            [Required(ErrorMessage = "First name is required")]
            public string Firstname { get; set; }
    
            [Required(ErrorMessage = "Last name is required")]
            public string Lastname { get; set; }
    
    
        }
    

    在注册时,这些都是使用ModelBinder映射的,并且一切都很好。但是,在“编辑我的详细信息”页面上,只能更新firstname、firstname和email。 每当视图回发并应用模型绑定时,我会得到一个警告用户名/密码是必需的字段。即使此时不需要。我想了两种方法来解决这个问题,我觉得这两种方法都不合适(但可能是错误的)。

    1:创建自定义视图模型。这将很好地工作,但数据注释将需要应用于这个视图模型,这意味着对模型和用户对象进行重复验证。

    2:包括renderd视图中的所有字段,并将它们发回。这有安全风险,看起来非常混乱,不能很好地扩展到复杂的视图模型。

    有人能为这种情况推荐一个最佳实践吗?

    2 回复  |  直到 10 年前
        1
  •  1
  •   Community T.Woody    7 年前

    最近也有类似的问题: Needing to copy properties before validation . 作为回应,我建议只在这个特定的操作中创建自定义的ModelBinder,我仍然认为这是一个最佳的解决方案。

        2
  •  0
  •   Pankaj Rupapara    10 年前
    DataType
    
    Specify the datatype of a property
    DisplayName
    
    specify the display name for a property.
    DisplayFormat
    
    specify the display format for a property like different format for Date proerty.
    Required
    
    Specify a property as required.
    ReqularExpression
    
    validate the value of a property by specified regular expression pattern.
    Range
    
    validate the value of a property with in a specified range of values.
    StringLength
    
    specify min and max length for a string property.
    MaxLength
    
    specify max length for a string property.
    Bind
    
    specify fields to include or exclude when adding parameter or form values to model properties.
    ScaffoldColumn
    
    specify fields for hiding from editor forms.
    

    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Web.Mvc;
    namespace Employee.Models
    {
    [Bind(Exclude = "EmpId")]
    public class Employee
    {
    [ScaffoldColumn(false)]
    public int EmpId { get; set; }
    [DisplayName("Employee Name")]
    [Required(ErrorMessage = "Employee Name is required")]
    [StringLength(100,MinimumLength=3)]
    public String EmpName { get; set; }
    [Required(ErrorMessage = "Employee Address is required")]
    [StringLength(300)]
    public string Address { get; set; }
    [Required(ErrorMessage = "Salary is required")]
    [Range(3000, 10000000,ErrorMessage = "Salary must be between 3000 and 10000000")]
    public int Salary{ get; set; }
    [Required(ErrorMessage = "Please enter your email address")]
    [DataType(DataType.EmailAddress)]
    [Display(Name = "Email address")]
    [MaxLength(50)]
    [RegularExpression(@"[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}", ErrorMessage = "Please enter correct email")]
    public string Email { get; set; }
    }
    }