朋友,
这个问题在过去2天真让我烦恼。我终于决定把它放在SO上。
我的模型中有以下属性
[Required]
[StringLength(20, MinimumLength = 4, ErrorMessage = "Length of this field should be between 3 and 20.")]
public string Password { get; set; }
[Required]
[StringLength(20, MinimumLength = 4, ErrorMessage = "Password Lenght should be between 4 and 20.")]
[Compare("Password", ErrorMessage = "Password did not match")]
[Display(Name = "Retype Password")]
public string RePassword { get; set; }
在razor中使用HTML创建相同的输入字段。
<div class="form-group">
@Html.LabelFor(m => m.Password, new { @class = "control-label col-md-4" })
<div class="col-md-4">
@Html.PasswordFor(m => m.Password, new { @class = "form-control", placeholder = "Password" })
</div>
<div class="col-md-4">
@Html.ValidationMessageFor(m => m.Password)
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.RePassword, new { @class = "control-label col-md-4" })
<div class="col-md-4">
@Html.PasswordFor(m => m.RePassword, new { @class = "form-control", placeholder = "Retype Password" })
</div>
<div class="col-md-4">
@Html.ValidationMessageFor(m => m.RePassword)
</div>
</div>
始终呈现以下HTML
<div class="form-group">
<label class="control-label col-md-4" for="Password">
Password
</label>
<div class="col-md-4">
<input id="Password" class="form-control" type="password" placeholder="Password" name="Password" data-val-required="The Password field is required." data-val-length-min="4" data-val-length-max="20" data-val-length="Length of this field should be between 3 and 20." data-val="true"></input>
</div>
<div class="col-md-4">
<span class="field-validation-valid help-block" data-valmsg-replace="true" data-valmsg-for="Password"></span>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-4" for="RePassword">
Retype Password
</label>
<div class="col-md-4">
<input id="RePassword" class="form-control" type="password" placeholder="Retype Password" name="RePassword" data-val-required="The Retype Password field is required." data-val-length-min="4" data-val-length-max="20" data-val-length="Password Lenght should be between 4 and 20." data-val-equalto-other="*.Password" data-val-equalto="'Retype Password' and 'Password' do not match." data-val="true"></input>
</div>
<div class="col-md-4">
<span class="field-validation-valid help-block" data-valmsg-replace="true" data-valmsg-for="RePassword"></span>
</div>
</div>
它无论我在ErrorMessage
属性Compare
中写什么,它data-val-equalto
属性的'Retype Password' and 'Password' do not match.
属性始终具有相同的值RePassword
< / p>
答案 0 :(得分:2)
这是一个错误:CompareAttribute does not use custom error messages已修复
下载固定的CompareAttributeAdapter http://aspnetwebstack.codeplex.com/Download/AttachmentDownload.ashx?ProjectName=aspnetwebstack&WorkItemId=1401&FileAttachmentId=755657 和RegisterAdapter
DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(System.ComponentModel.DataAnnotations.CompareAttribute), typeof(TheFixed.CompareAttributeAdapter));
答案 1 :(得分:1)
这个答案已经很晚了所以我相信你已经弄明白了,因为我没有找到任何直接回答这个问题的在线解决方案,所以我决定分享我为了那些寻求解决方案的人所做的工作。 :
创建自定义比较属性并将自己的错误应用于它。
public class CustomCompareAttribute : CompareAttribute
{
public CustomCompareAttribute(string otherProperty, string ErrorCode) : base(otherProperty)
{
ErrorMessage = CustomMessage(ErrorCode);
}
private static string CustomMessage(string ErrorCode)
{
string ErrorMsg = ErrorCode;
//ErrorMsg = Translate.Item(ErrorCode); <-- My logic from database
return ErrorMsg;
}
}
然后在您的模型中,您只需添加自定义注释(省略“属性”一词):
[Required]
[StringLength(20, MinimumLength = 4, ErrorMessage = "Password Lenght should be between 4 and 20.")]
[CustomCompare("Password", "My own message: Passwords do not match!")] // <-- Use your new Custom Attribute here
[Display(Name = "Retype Password")]
public string RePassword { get; set; }