When a user clicks a button in my application that causes validation errors, is there a way to have the page scroll to the location of the Validation Summary if there are errors? I need to do this for both cases:
Is there an easy way to accomplish this in MVC?
The ValidationSummary() extension method displays a summary of all validation errors on a web page as an unordered list element. It can also be used to display custom error messages.
The ValidationSummary class is used to summarize the error messages from all validators on a Web page in a single location. You can summarize the error messages from a group of validators on a Web page by assigning the ValidationSummary control to a validation group by setting the ValidationGroup property.
You can use the standard ASP.NET MVC ValidationSummary method to render a placeholder for the list of validation error messages. The ValidationSummary() method returns an unordered list (ul element) of validation messages that are in the ModelStateDictionary object.
To display a custom error message, first of all, you need to add custom errors into ModelState in the appropriate action method. As you can see in the above code, we have added a custom error message using the ModelState. AddModelError() method.
jquery scroll to specific div position in case of error and focus <-- This post has a similar requirement - the stated solution is a js function firing on dom-load that scrolls the window to the desired position. Correctly implemented, this could create a solution for both client & server side validation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With