假设这个模型:
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; }
}
好的,每件事情都与这个观点完美配合:
@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>
}
所以我需要使用一些通用模型:
public class ViewModel<T> {
public T MainModel { get; set; }
public ViewPart ViewPart { get; set; }
}
然后我通过查看此通用视图并将视图更改为:
@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>
}
然后我在compare
验证时出现问题,因为您在模型比较验证中看到必须将ConfirmPassword
与NewPassword
进行比较,但是ConfirmPassword
与OldPassword
进行了比较1}}那是如此有线。我想这可能是因为名字或元素的变化,所以我试试
[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.")]
但是没有一个没有用,问题是什么?以及如何将比较验证与此新模型一起使用?有什么办法吗?
答案 0 :(得分:1)
你还没有提供脚本,下面我修改了编码
型号:
//[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>