InterviewSolution
| 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() SignatureMvcHtmlString 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 :
|
|