Question

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!

Was it helpful?

Solution

Resolved it by changing to a custom message in the catch expression of HttpPost method in the controller rather than in the view.

[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);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top