ASP.NET MVC3比较验证无法正常工作 [英] ASP.NET MVC3 Compare Validation not worked properly
问题描述
假设这个模型:
public class ChangePasswordModel {
[Required(ErrorMessage = CustomRegex.RequiredErMsg)]
[DataType(DataType.Password)]
[Display(Name = "Current password")]
public string OldPassword { get; set; }
[Required(ErrorMessage = CustomRegex.RequiredErMsg)]
[RegularExpression(CustomRegex.PasswordRX, ErrorMessage = CustomRegex.PasswordErMsg)]
[DataType(DataType.Password)]
[Display(Name = "New password")]
public string NewPassword { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm new password")]
[Compare("NewPassword", ErrorMessage = "The new password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
}
确定,每一件事情完美工作这一观点:
OK, every thing worked perfectly with this view:
@model ChangePasswordModel
@{
ViewBag.Title = "Profile";
}
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Change Password</legend>
<dl>
<dt>@Html.LabelFor(model => model.OldPassword)</dt>
<dd>
@Html.EditorFor(model => model.OldPassword)
@Html.ValidationMessageFor(model => model.OldPassword, null, new { @class = "invalid-side-note" })
</dd>
<dt>@Html.LabelFor(model => model.NewPassword)</dt>
<dd>
@Html.EditorFor(model => model.NewPassword)
@Html.ValidationMessageFor(model => model.NewPassword, null, new { @class = "invalid-side-note" })
</dd>
<dt>@Html.LabelFor(model => model.ConfirmPassword)</dt>
<dd>
@Html.EditorFor(model => model.ConfirmPassword)
@Html.ValidationMessageFor(model => model.ConfirmPassword, null, new { @class = "invalid-side-note" })
</dd>
</dl>
<input type="submit" value="Save" class="button red inframebutton" />
</fieldset>
}
所以,我需要使用一些通用模型:
So I need use some generic model :
public class ViewModel<T> {
public T MainModel { get; set; }
public ViewPart ViewPart { get; set; }
}
然后我通过查看此通用视图,并改变视图:
Then I passed to view this generic view and changed view as:
@model ViewModel<ChangePasswordModel>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Change Password</legend>
<dl>
<dt>@Html.LabelFor(model => model.MainModel.OldPassword)</dt>
<dd>
@Html.EditorFor(model => model.MainModel.OldPassword)
@Html.ValidationMessageFor(model => model.MainModel.OldPassword, null, new { @class = "invalid-side-note" })
</dd>
<dt>@Html.LabelFor(model => model.MainModel.NewPassword)</dt>
<dd>
@Html.EditorFor(model => model.MainModel.NewPassword)
@Html.ValidationMessageFor(model => model.MainModel.NewPassword, null, new { @class = "invalid-side-note" })
</dd>
<dt>@Html.LabelFor(model => model.MainModel.ConfirmPassword)</dt>
<dd>
@Html.EditorFor(model => model.MainModel.ConfirmPassword)
@Html.ValidationMessageFor(model => model.MainModel.ConfirmPassword, null, new { @class = "invalid-side-note" })
</dd>
</dl>
<input type="submit" value="Save" class="button red inframebutton" />
</fieldset>
}
然后我有问题比较
验证你的模型中看到对比验证必须比较 ConfirmPassword
与 NEWPASSWORD
,但这一新的变化 ConfirmPassword
与 OldPassword更改
这是如此有线。我想,这也许是因为元素的名称或ID的变化,所以我尝试
Then I have a problem with compare
validation as you see in model compare validation must compare ConfirmPassword
with NewPassword
but with this new changes the ConfirmPassword
compared with OldPassword
that's so wired. I think this is maybe because of change in names or Ids of elements so I try
[Compare("MainModel.NewPassword", ErrorMessage = "The new password and confirmation password do not match.")]
和
[Compare("MainModel_NewPassword", ErrorMessage = "The new password and confirmation password do not match.")]
但没有人没有工作,有什么问题呢?我如何使用这个新的模式比较验证?有什么办法?
but none of them not worked, what's the problem? and how can I use compare validation with this new model? is there any way?
推荐答案
您还没有指向的脚本,下面我体改编码
You have not refered the script, below I have modifed the coding
型号:
//[Required(ErrorMessage = "CustomRegex.RequiredErMsg")]
[Required(ErrorMessage = "Current password is Required")]
[DataType(DataType.Password)]
[Display(Name = "Current password")]
public string OldPassword { get; set; }
[Required(ErrorMessage = "New password is Required")]
[DataType(DataType.Password)]
[Display(Name = "New password")]
public string NewPassword { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm new password")]
[Compare("NewPassword", ErrorMessage = "The new password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
}
查看:
@model test.Models.password
<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
@using (Html.BeginForm())
{
<fieldset>
<legend>Change Password</legend>
<dl>
<dt>@Html.LabelFor(model => model.OldPassword)</dt>
<dd>
@Html.EditorFor(model => model.OldPassword)
@Html.ValidationMessageFor(model => model.OldPassword, null, new { @class = "invalid-side-note" })
</dd>
<dt>@Html.LabelFor(model => model.NewPassword)</dt>
<dd>
@Html.EditorFor(model => model.NewPassword)
@Html.ValidationMessageFor(model => model.NewPassword, null, new { @class = "invalid-side-note" })
</dd>
<dt>@Html.LabelFor(model => model.ConfirmPassword)</dt>
<dd>
@Html.EditorFor(model => model.ConfirmPassword)
@Html.ValidationMessageFor(model => model.ConfirmPassword, null, new { @class = "invalid-side-note" })
</dd>
</dl>
<input type="submit" value="Save" class="button red inframebutton" />
</fieldset>
这篇关于ASP.NET MVC3比较验证无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!