Search code examples
asp.net-mvc-3asp.net-mvc-validationasp.net-mvc-controller

How to return an error to ValidationSummry in MVC 3


I have an idea which I dont know how to implement. In MVC 3 controller I have an if statement, and I want the controller to return an error message if the if statement fails. And I have a ValidationSummery on that page, so its better if it will catch the error message and present it..

How do I do this?

Thanks


Solution

  • In the controller you can add an error message by

    ModelState.AddModelError(string key, string errorMessage);