MVC3 CompareAttribute,客户端的bug [英] MVC3 CompareAttribute, client-side bug
问题描述
我使用MVC3,我想有登录表单,并在同一页面上注册表单。为了实现这个目标我建LogInRegisterViewModel如下:
I am using MVC3 and I want to have LogIn form and Register form on the same page. To achieve that I built LogInRegisterViewModel as following:
public class LogInRegisterViewModel
{
public LogInViewModel LogIn { get; set; }
public RegisterViewModel Register { get; set; }
}
这给了我什么,我想(在同一屏幕上两种形式),并发布到正确的控制器,并返回数据和表格显示错误(如果有的话)。我唯一的问题是CompareAttribute,我上面有ConfirmPassword财产在我RegisterViewModel:
It gives me what I want (two forms on the same screen) and posts the data to correct controllers and returns and displays errors for forms (if any). The only problem I have is with CompareAttribute that I have above ConfirmPassword property in my RegisterViewModel:
public class RegisterViewModel
{
[Required]
[Display(Name = "Friendly user name")]
public string UserName { get; set; }
[Required]
[Display(Name = "E-mail address")]
public string Email { get; set; }
[Required]
[DataType(DataType.Password)]
[Display(Name = "Password")]
[StringLength(16, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "Passwords do not match.")]
public string ConfirmPassword { get; set; }
}
客户端密码绝不等于(〜我总是从一条消息,告诉我说,他们是不相等的比较验证错误),即使他们是(我知道这一点)。 HTML浏览器是:
Client-side the passwords are never equal (~ I always get a validation error from Compare with a message telling me that they are not equal), even if they are (I am sure about that). HTML in the browser is:
<div class="editor-label">
<label for="Register_Password">Password</label>
</div>
<div class="editor-field">
<input class="valid" data-val="true" data-val-length="The Password must be at least 6 characters long." data-val-length-max="16" data-val-length-min="6" data-val-required="The Password field is required." id="Register_Password" name="Register.Password" type="password">
<span class="field-validation-valid" data-valmsg-for="Register.Password" data-valmsg-replace="true"></span>
</div>
<div class="editor-label">
<label for="Register_ConfirmPassword">Confirm password</label>
</div>
<div class="editor-field">
<input class="input-validation-error" data-val="true" data-val-equalto="Passwords do not match." data-val-equalto-other="*.Password" id="Register_ConfirmPassword" name="Register.ConfirmPassword" type="password">
<span class="field-validation-error" data-valmsg-for="Register.ConfirmPassword" data-valmsg-replace="true"><span class="" generated="true" for="Register_ConfirmPassword">Passwords do not match.</span></span>
</div>
我有一种感觉,它是所有关于这个属性:
数据-VAL-equalto-其他=*密码
I have a feeling that it's all about this attribute: data-val-equalto-other="*.Password"
CompareAttribute工作正常,当我直接使用RegisterViewModel。任何人有前来到这个?它是一个错误还是我做错了什么?如何使比较在我的案件的工作?
CompareAttribute works fine, when I use RegisterViewModel directly. Anyone has come into this before? Is it a bug or am I doing something wrong? How to make Compare to work in my case?
推荐答案
它应该与 [比较(密码的ErrorMessage =密码不匹配。)$ C工作$ C>属性,但看来这是真的在jquery.validate.unobtrusive.js文件中的错误。问题是,在这个code:
It should work with the [Compare("Password", ErrorMessage = "Passwords do not match.")]
attribute but it seems this is really a bug in the jquery.validate.unobtrusive.js file. The problem is in this code:
adapters.add("equalto", ["other"], function (options) {
var prefix = getModelPrefix(options.element.name),
other = options.params.other,
fullOtherName = appendModelPrefix(other, prefix),
element = $(options.form).find(":input[name=" + fullOtherName + "]")[0];
setValidationValues(options, "equalTo", element);
});
所以它试图找到其他控制与JQuery的找方法。但。 点
字符不会在fullOtherName变量转义(在你的情况下,将包含以下内容:Register.Password
)的在这个<一个描述href=\"http://stackoverflow.com/questions/605630/how-to-select-html-nodes-by-id-with-jquery-when-the-id-contains-a-dot\">SO问题。这就是为什么它的工作原理,当你只使用RegisterViewModel直接,因为再有就是在名称中没有点的原因。
So it tries to find the other control with the JQuery find method. But the "." dot
character is not escaped in the fullOtherName variable (in your case it will contain: "Register.Password"
) as described in this SO question. That is the reason why it works when you only use RegisterViewModel directly because then there is no dots in the names.
要解决它,你需要添加一行到appendModel preFIX功能:
To fix it you need to add one line to the appendModelPrefix function:
//original
function appendModelPrefix(value, prefix) {
if (value.indexOf("*.") === 0) {
value = value.replace("*.", prefix);
}
return value;
}
//fixed
function appendModelPrefix(value, prefix) {
if (value.indexOf("*.") === 0) {
value = value.replace("*.", prefix);
}
value = value.split('.').join('\\.');
return value;
}
这篇关于MVC3 CompareAttribute,客户端的bug的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!