1.

What is Validation summary in MVC

Answer»

Validation Summary is used to visualise the error MESSAGE in the unordered list which is in ModelStateDictionary object.we can DISPLAY the validation error of each property which is defined under modal

ValidationSummary() Signature

MvcHtmlString ValidateMessage(bool excludePropertyErrors, string message, Object htmlAttributes)

If we want to show error message summary of the field which is defined under the modal then specify excludePropertyErrors = FALSE.

To display field error in the validation summary

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

We can also display a custom error message using ValidationSummary. If Student name exists in the database then it will display the message.

To display a custom error message, first of all, you NEED to ADD custom errors into the ModelState in the appropriate action method.

Eg.

if (ModelState.IsValid) { bool nameAlreadyExists = * check database * if(nameAlreadyExists) { ModelState.AddModelError(string.Empty, "Student Name already exists."); return View(std); } }

In this Validation Summary will automatically generate the error message into the modal state

we have to add the custom error messages using the ModelState.AddModelError method

The Different Validation overloading methods are :

  • ValidationSummary(HtmlHelper, Boolean, String, Object): It Returns an unordered list of validation messages that are in the modelstatedictionary object and optionally displays only model-level errors.
  • ValidationSummary(HtmlHelper, Boolean, String, IDictionary<String,Object>): it will worked same as the ValidationSummary(HtmlHelper, Boolean, String, Object).
  • ValidationSummary(HtmlHelper, String, Object)
  • ValidationSummary(HtmlHelper, String, IDictionary<String,Object>)
  • ValidationSummary(HtmlHelper, Boolean, String)
  • ValidationSummary(HtmlHelper, String)
  • ValidationSummary(HtmlHelper, Boolean)
  • ValidationSummary(HtmlHelper)


Discussion

No Comment Found