我应该在WEB.UI Models文件夹中验证吗?

时间:2013-08-06 03:55:25

标签: asp.net-mvc-3

我已经完成了自定义成员链接: http://www.brianlegg.com/post/2011/05/09/Implementing-your-own-RoleProvider-and-MembershipProvider-in-MVC-3.aspx

我的搜索有两个项目:CameraStore.Domain和CameraStore.WebUI。在CameraStore.Domain项目中,我有一个名为User的实体,如下所示:

public int UserID { get; set; }
    [Required]
    [Display(Name = "User name")]
    public string Username { get; set; }

    [Required]
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [Required]
    [Display(Name = "Name")]
    public string Name { get; set; }

    [Required]
    [Display(Name = "City")]
    public string City { get; set; }

    [Required]
    [Display(Name = "Ward")]
    public string Ward { get; set; }

    [Required]
    [Display(Name = "User name")]
    public string Address { get; set; }

    [Required]
    [DataType(DataType.PhoneNumber)]
    [Display(Name = "Phone")]
    public string Phone { get; set; }

    [Required]
    [DataType(DataType.EmailAddress)]
    [Display(Name = "Email address")]
    public string Email { get; set; }

    public int RoleID { get; set; }
    public virtual Role Role { get; set; }
    public virtual ICollection<Order> Order { get; set; }

在我的Register Controller方法中,我想将用户插入到我的数据库中。但我有字段ConfirmPassword,如果此属性在User Entity类中从未有过,我该如何有效。我在我的视图中使用了一个名为AccountUserModels的类来返回2个下拉列表,我可以将属性ConfirmPassword添加到此类中。我不想通过javascript验证我在视图中提交的ConfirmPassword。

1 个答案:

答案 0 :(得分:0)

如果我没错,你想比较密码吗?

试试这个

   [Required(ErrorMessage = "New password is required.")]
        [DataType(DataType.Password)]
        [StringLength(20, MinimumLength = 5)]
        [Display(Name = "New password:")]
        public string NewPassword { get; set; }

        [Required(ErrorMessage = "Confirm password is required.")]
        [DataType(DataType.Password)]
        [StringLength(20, MinimumLength = 5)]
        [Compare("NewPassword")]
        [Display(Name = "Confirm password:")]
        public string RePassword { get; set; }

<强>控制器

[HttpPost]
public ActionResult Register(RegisterViewModel model)
{
    if (!ModelState.IsValid)
    {
        return View(model);
    }


    return RedirectToAction("Success");
}