假设这个模型:
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
它的比较就非常紧密了。我认为这可能是因为元素的名称或 ID 发生了变化,所以我尝试
[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.")]
但他们都没有工作,有什么问题?以及如何使用此新模型进行比较验证?有什么办法吗?