Show ValidationSummary MVC3 as “alert-error” Bootstrap

后端 未结 16 961
情书的邮戳
情书的邮戳 2021-01-30 00:51

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         


        
16条回答
  •  离开以前
    2021-01-30 01:21

    This answer is based on RickB's one

    • Updated for the latest bootstrap ==>> alert-error doesn't exist in favor of 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.

提交回复
热议问题