I want to show a ValidationSummary mcv3 with \"alert-error\" Bootstrap styling.
I\'m using a Razor view, and I show model errors with this code:
@Html.V
Updated for the latest bootstrap ==>> doesn't exist in favor of alert-error
alert-danger
.
Works for all Validation Errors not only Key String.Empty ("")
For anyone using Bootstrap 3 and trying to get nice looking alerts:
if (ViewData.ModelState.Keys.Any(k=> ViewData.ModelState[k].Errors.Any())) {
@Html.ValidationSummary(false, "Errors: ")
}
The solution provided by RickB works only on manually added errors on (String.Empty key) but not on those generated by ModelState (normally this gets triggered first via javascript but it's always a good practice to have a fallback if (for example) the Html.ValidationMessageFor
is missing or many other situations.