0

How do I check in .cshtml page if @Html.ValidatonSummary(true) contains any validation errors.

If a user enters invalid values and submits the form by clicking a button, the validation error is displayed. I want to be able to check if there are any error message and then modify that error message while displaying. Something like this in the cshtml page,

if @Html.ValidationSummary(true) returns error message
then @Html.ValidationSummary(MyResourceFile.InvalidEntries) 

Thanks!

4

1 回答 1

0

通过在控制器中而不是在视图中更改为 HttpPost 方法的 catch 表达式中的自定义消息来解决它。

[HttpPost]      
public ActionResult Validate(LoginModel model, string returnUrl) {

 try {
     if (!ModelState.IsValid) {
     throw new AuthenticationException(CustomErrorRes.InvalidEntry);
        }

 catch (Exception e) {

      ModelState.AddModelError(string.Empty, CustomErrorRes.InvalidEntry);
      TempData[ACCOUNT_LOGIN_ERROR] = CustomErrorRes.InvalidEntry;
      return RedirectToAction("Validate");
   }
 return RedirectToLocal(returnUrl);
}
于 2013-07-15T17:19:40.760 回答