1.

What is Validation Summary in MVC?

Answer»

Validation Summary is USED to display the LIST error messages of a page at fixed ONE place. If it summarises all the error messages of a page and shows them at once after the submit button is CLICKED and page is validated. 

MVC have provided the helper class display a summary of all validation errors of a page as:

@Html.ValidationSummary

To display validation summary we need to add this helper class in our view as:

@Html.ValidationSummary(false, "", new { @class = "text-danger" })

We can also display the CUSTOM error message using ValidationSummary. For this, we need to add the custom error messages to the ModelState in the respective action method.

Example:

if (ModelState.IsValid) {      bool nameExists = * check here *            if(nameExists)     {         ModelState.AddModelError(string.Empty, "Name already exists.");             return View(name);     }}


Discussion

No Comment Found