これは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、長さチェック エラーがインライン表示になるので、確認も同じようにしたいと思います。
ありがとう、デイブ