如何在ASP中正确实现“确认密码”。净MVC 3 ?

时间:2020-12-19 03:29:51

There's already an answered question about the same subject but as it's from '09 I consider it outdated.

这个问题已经有了答案,但由于它是09年的,我认为它已经过时了。

How to properly implement "Confirm Password" in ASP.NET MVC 3?

如何在ASP中正确实现“确认密码”。净MVC 3 ?

I'm seeing a lot of options on the Web, most of them using the CompareAttribute in the model like this one

我在Web上看到了很多选项,大多数都使用了这个模型中的CompareAttribute

The problem is that definitely ConfirmPassword shound't be in the model as it shouldn't be persisted.

问题是,肯定确认密码shound不在模型中,因为它不应该被持久化。

As the whole unobstrusive client validation from MVC 3 rely on the model and I don't feel like putting a ConfirmPassword property on my model, what should I do?

由于MVC 3中完整的客户端验证依赖于模型,我不想在我的模型上设置一个确认密码属性,我应该怎么做呢?

Should I inject a custom client validation function? If so.. How?

我应该注入自定义客户端验证功能吗?如果是这样. .如何?

2 个解决方案

#1


73  

As the whole unobstrusive client validation from MVC 3 rely on the model and I don't feel like putting a ConfirmPassword property on my model, what should I do?

由于MVC 3中完整的客户端验证依赖于模型,我不想在我的模型上设置一个确认密码属性,我应该怎么做呢?

A completely agree with you. That's why you should use view models. Then on your view model (a class specifically designed for the requirements of the given view) you could use the [Compare] attribute:

A完全同意你的观点。这就是为什么您应该使用视图模型。然后在您的视图模型(为给定视图的需求专门设计的类)上,您可以使用[Compare]属性:

public class RegisterViewModel
{
    [Required]
    public string Username { get; set; }

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

    [Compare("Password", ErrorMessage = "Confirm password doesn't match, Type again !")]
    public string ConfirmPassword { get; set; }
}

and then have your controller action take this view model

然后让你的控制器动作采用这个视图模型

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

    // TODO: Map the view model to a domain model and pass to a repository
    // Personally I use and like AutoMapper very much (http://automapper.codeplex.com)

    return RedirectToAction("Success");
}

#2


3  

Take a look at the default VS2010 template for a MVC3 app.

看看MVC3应用程序的默认VS2010模板。

It contains a RegisterModel (a 'ViewModel') that contains the Password and ConfirmPassword properties. The validation is set on the ConfirmPassword.

它包含一个RegisterModel(“ViewModel”),其中包含密码和ConfirmPassword属性。验证设置在ConfirmPassword上。

So the answer is that the Models in MVC don't have to be (usually aren't) the same as your business Models.

因此,答案是MVC中的模型不必(通常不是)与您的业务模型相同。

#1


73  

As the whole unobstrusive client validation from MVC 3 rely on the model and I don't feel like putting a ConfirmPassword property on my model, what should I do?

由于MVC 3中完整的客户端验证依赖于模型,我不想在我的模型上设置一个确认密码属性,我应该怎么做呢?

A completely agree with you. That's why you should use view models. Then on your view model (a class specifically designed for the requirements of the given view) you could use the [Compare] attribute:

A完全同意你的观点。这就是为什么您应该使用视图模型。然后在您的视图模型(为给定视图的需求专门设计的类)上,您可以使用[Compare]属性:

public class RegisterViewModel
{
    [Required]
    public string Username { get; set; }

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

    [Compare("Password", ErrorMessage = "Confirm password doesn't match, Type again !")]
    public string ConfirmPassword { get; set; }
}

and then have your controller action take this view model

然后让你的控制器动作采用这个视图模型

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

    // TODO: Map the view model to a domain model and pass to a repository
    // Personally I use and like AutoMapper very much (http://automapper.codeplex.com)

    return RedirectToAction("Success");
}

#2


3  

Take a look at the default VS2010 template for a MVC3 app.

看看MVC3应用程序的默认VS2010模板。

It contains a RegisterModel (a 'ViewModel') that contains the Password and ConfirmPassword properties. The validation is set on the ConfirmPassword.

它包含一个RegisterModel(“ViewModel”),其中包含密码和ConfirmPassword属性。验证设置在ConfirmPassword上。

So the answer is that the Models in MVC don't have to be (usually aren't) the same as your business Models.

因此,答案是MVC中的模型不必(通常不是)与您的业务模型相同。