仅更新模型的一部分,验证问题?

时间:2010-08-24 15:14:14

标签: c# asp.net-mvc linq-to-sql model

我有一个注册页面,用户可以在其中输入他的FirstName,LastName,Email和Password,以及其他字段。

我已将验证属性绑定到此模型(称为“User”并通过LINQtoSQL创建)并且一切正常。

型号代码:

[MetadataType(typeof(UserValidation))]
public partial class User { }

[Bind(Exclude = "UserID")]
[PropertiesMustMatch("Password", "ConfirmPassword", ErrorMessage = "The password and confirm password don't match.")]
public class UserValidation 
{
    [Required(ErrorMessage = "First name required"), StringLength(20, MinimumLength=3, ErrorMessage = "Must be between 3 and 20 characters")]
    public string FirstName { get; set; }

    [Required(ErrorMessage = "Last name required"), StringLength(20, MinimumLength = 3, ErrorMessage = "Must be between 3 and 20 characters")]
    public string LastName { get; set; }

    [Required(ErrorMessage = "Email address required"), RegularExpression("^[a-z0-9_\\+-]+(\\.[a-z0-9_\\+-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*\\.([a-z]{2,4})$", ErrorMessage = "Must be a valid email address")]
    public string Email { get; set; }

    [Required(ErrorMessage = "Password required"), StringLength(20, MinimumLength = 6, ErrorMessage = "Password must be between 6 and 20 characters")]
    public string Password { get; set; }

    [Required(ErrorMessage = "Confirm password required"), StringLength(20, MinimumLength = 6, ErrorMessage = "Password must be between 6 and 20 characters")]
    public string ConfirmPassword { get; set; }

}

注册并登录后,我希望用户能够在一个视图中编辑他们的FirstName,LastName和Email(我们称之为“帐户”字段),在另一个视图中编辑“密码”。这就是我的问题所在。

当我通过注册中使用的相同模型(“用户”)提交表单更新帐户字段数据时,IsValid方法会针对缺少的“密码”字段发出ModelState错误。

控制器代码:

    //
    // GET /Talent/Account
    public ActionResult Account()
    {
        string cookieUser = User.Identity.Name;
        User user = userRepository.GetUserByEmail(cookieUser);
        return View(user);
    }

    // POST /Talent/Account
    [HttpPost]
    public ActionResult Account(User model)
    {

        if (ModelState.IsValid)
        {
    // do something
            ModelState.AddModelError("", "All good.. "+ model.FirstName + " - " + model.LastName);
        }

        return View(model);
    }

我怎样才能解决这个问题?最佳实践等...

1 个答案:

答案 0 :(得分:4)

在Model-View-ViewModel(MVVM)方式中,您应该创建两个ViewModel:一个用于创建新帐户,另一个用于编辑名称字段而不需要密码。

将验证属性应用于ViewModel,而不是直接应用于模型。

一些示例包装器ViewModel类:

        [Bind(Exclude = "UserID")]    
        public class UserForAccountEdit
        {
            public User UserAccount { get; set; }

            [Required(ErrorMessage = "First name required"), StringLength(20, MinimumLength=3, ErrorMessage = "Must be between 3 and 20 characters")]  
            public string FirstName  
            {  
              get 
                   { return UserAccount.FirstName };  
              set
                   { UserAccount.FirstName = value; }
            }  

            ...
        }    



        [Bind(Exclude = "UserID")]
        [PropertiesMustMatch("Password", "ConfirmPassword", ErrorMessage = "The password and confirm password don't match.")]
        public class UserForAccountCreation
        {
            public User UserAccount { get; set; }

            [Required(ErrorMessage = "First name required"), StringLength(20, MinimumLength=3, ErrorMessage = "Must be between 3 and 20 characters")]  
            public string FirstName  
            {  
              get 
                   { return UserAccount.FirstName };  
              set
                   { UserAccount.FirstName = value; }
            }  

            ...
        }