比较(密码)属性 [英] Compare (password) attribute

查看:223
本文介绍了比较(密码)属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个视图模型使用下面的code的新用户。 用户类只包含两个属性(简体现在),我会坚持到数据库中;视图模型增加了一个比较密码字段,这是只有在视图中使用。我想preFER有视图模型直接使用用户级,而不是重复尽在用户中定义的字段。

I'd like to create a view model for a new user using the code below. The "User" class contains just the two properties (simplified for now) that I will persist to the database; the view model adds a "compare password" field, which is only used in the view. I'd prefer to have the view model use the "User" class directly, rather than repeating all of the fields defined in "User".

我的问题是我怎么正确地引用user.password的在[比较]属性为ComparePassword字段?

My question is how do I properly reference "User.Password" in the [Compare] attribute for the "ComparePassword" field?

public class User
{
   [Required]
   public string UserName { get; set; }

   [Required]
   [DisplayName("Password")]
   [DataType(DataType.Password)]
   public string Password { get; set; }
}
public class NewUserViewModel
{
    public User User { get; set; }

    [Required]
    [DataType(DataType.Password)]
    [DisplayName("Re-enter Password")]
    [Compare("Password", ErrorMessage="Passwords must match")]
    public string ComparePassword { get; set; }
}

这是获取密码和ComparePassword生成的HTML代码如下。

The HTML that gets generated for "Password" and "ComparePassword" is below.

<input class="text-box single-line password" 
  data-val="true" 
  data-val-required="The Password field is required." 
  id="User_Password" 
  name="User.Password" 
  type="password" value="" />

<input class="text-box single-line password" 
  data-val="true" 
  data-val-equalto="Passwords must match" 
  data-val-equalto-other="*.Password"
  data-val-required="The Re-enter Password field is required." 
  id="ComparePassword" 
  name="ComparePassword" 
  type="password" value="" />

关键是如何在数据-VAL-equalto-其他是由JavaScript的处理。如果我使用密码 USER_PASSWORD 什么也没有发生 - 不进行校验。如果我使用 user.password的进行检查,但总是失败。

The key is how the "data-val-equalto-other" is handled by the Javascript. If I use "Password" or "User_Password" nothing happens - no check is performed. If I use "User.Password" the check is performed but always fails.

我没有真正的问题在jQuery中直接这样做,但将preFER使用[比较]属性,如果在所有可能的。

I have no real problem doing this directly in jQuery, but would prefer to use the [Compare] attribute if at all possible.

推荐答案

刚刚发现通过计算器和Microsoft Connect上的答案是:

Just found the answer via StackOverflow and Microsoft Connect:

请参阅:

<一个href=\"http://connect.microsoft.com/VisualStudio/feedback/details/665793/jquery-unobtrusive-validate-equalto-fails-with-compare-attribute\">http://connect.microsoft.com/VisualStudio/feedback/details/665793/jquery-unobtrusive-validate-equalto-fails-with-compare-attribute

<一href=\"http://stackoverflow.com/questions/5117458/jquery-1-5-breaks-compare-validate-jquery-validate-1-8\">JQuery 1.5休息比较验证(JQuery验证1.8)

要summerize,它看起来像在与MVC3附带的jquery.validate.unobtrusive文件中的错误。解决办法是改变以下线的jquery.validate.unobtrusive文件。

To summerize, it looks like a bug in the jquery.validate.unobtrusive file that came with MVC3. The workaround is changing the following line in the jquery.validate.unobtrusive file.

element = $(options.form).find(":input[name=" + fullOtherName + "]")[0];

element = $(options.form).find(":input[name=" + fullOtherName.replace(".", "\\.") + "]")[0];

在Microsoft连接,它说MS已经固定,但我不能找到链接到新版本。不管怎么说,这对我的作品在此期间。希望它能帮助

On Microsoft Connect, it says MS has fixed it, but i couldnt find the link to the new version. Anyways, this works for me in the meantime. Hope it helps

这篇关于比较(密码)属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆