这是一个与 将 ValidationResult 分配给特定字段类似的问题?
我的视图模型如下所示:
[DisplayName("Password")]
[Required(ErrorMessage = "Password is required")]
[StringLength(3, ErrorMessage = "Password length Should be less than 3")]
public string Password { get; set; }
[DisplayName("Confirm Password")]
[Required(ErrorMessage = "Confirm Password is required")]
[StringLength(3, ErrorMessage = "Confirm Password length should be less than 3")]
public string ConfirmPassword { get; set; }
public static ValidationResult ExtendedValidation(ManageUserViewModel t)
{
if (t.Password == t.ConfirmPassword)
return ValidationResult.Success;
else
return new ValidationResult("Your passwords must match", new[] { "ConfirmPassword" });
}
我的观点是这样的:
@Html.ValidationSummary(true, "The user details were not saved, please see the validation results below")
@using (Html.BeginForm("Save", "Users", FormMethod.Post))
{
<div class="formField">
@Html.LabelFor(model => model.Password)
@Html.TextBoxFor(model => model.Password)
@Html.ValidationMessageFor(model => model.Password)
</div>
<div class="formField">
@Html.LabelFor(model => model.ConfirmPassword)
@Html.TextBoxFor(model => model.ConfirmPassword)
@Html.ValidationMessageFor(model => model.ConfirmPassword)
</div>
但是当我希望它在线显示时,我的自定义验证错误会显示在页面顶部。
EG,长度检查错误导致在线显示,我希望我的确认也这样做。
谢谢,戴夫