Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scroll to Validation Summary - ASP.net MVC

Tags:

asp.net-mvc

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:

  1. Client Validation prevents the page from submitting
  2. Server Validation redisplays the web page

Is there an easy way to accomplish this in MVC?

like image 746
Dismissile Avatar asked Feb 20 '12 15:02

Dismissile


People also ask

What is validation summary 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.

What is validation summary in asp net?

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.

How can show validation message in ASP NET MVC?

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.

How do I create a custom error message in ValidationSummary?

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.


1 Answers

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.

like image 70
Jim Patterson Avatar answered Nov 15 '22 05:11

Jim Patterson