Showing all error messages at one place is good practice and improves performance as well. Handling each error separately will increase the network traffic. To show all error messages at one place we need to make a few changes in our application.
To display all errors at one place, use ValidationSummary() HTML helper.
@Html.ValidationSummary(false, "Please fix the following errors and then submit the form")
To display an asterisk, next to each the field that has failed validation, modify ValidationMessageFor() HTML helper as shown below.
@Html.ValidationMessageFor(model => model.Name, "*")
You can change the look and feel by modifying the Site.css file. Below are the styles which you can modify as per your requirement.
field-validation-error
input.input-validation-error
validation-summary-errors
Search these in Site.css file and make changes and see the HTML rendered in the web page.
No comments:
Post a Comment