Show ValidationSummary MVC3 as “alert-error” Bootstrap

后端 未结 16 877
情书的邮戳
情书的邮戳 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:20

    TwitterBootstrapMVC takes care of this one with just one line:

    @Html.Bootstrap().ValidationSummary()
    

    Important, to assure that it behaves the same during the server side and client side (unobtrissive) validation, you need to include a javaScript file that takes care of that.

    You can customize your Validation helper with extension methods however you see fit.

    Disclaimer: I'm the author of TwitterBootstrapMVC. Using it with Bootstrap 3 requires a license.

    0 讨论(0)
  • 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())) { 
        <div class="alert alert-danger">
            <button class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
            @Html.ValidationSummary(false, "Errors: ")
        </div>
    }
    

    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.

    0 讨论(0)
  • 2021-01-30 01:21

    This solution uses Sass to make it work but you could achieve the same thing with basic css. To make this work with client side validation we cant rely on checking the ModelState since that assumes a postback has occurred. The out-of-the-box mvc client side validation already makes things visible at the right time so let it do its thing and simply style the list items in the validation summary to render like bootstrap alerts.

    Razor markup:

    @Html.ValidationSummary(false, null, new { @class = "validation-summary-errors-alerts" })
    

    Sass

    .validation-summary-errors-alerts{
    ul{
        margin: 0;
        list-style: none;
        li{
            @extend .alert;
            @extend .alert-danger;
        }
    }}
    

    The css that produced for my project looked like this - yours will be different:

    .validation-summary-errors-alerts ul li {
    min-height: 10px;
    padding: 15px 20px 15px 62px;
    position: relative;
    border: 1px solid #ca972b;
    color: #bb7629;
    background-color: #fedc50;
    font-family: Arial;
    font-size: 13px;
    font-weight: bold;
    text-shadow: none;}
    
    0 讨论(0)
  • 2021-01-30 01:29
    @Html.ValidationSummary("", new { @class = "alert alert-danger" })
    
    0 讨论(0)
  • 2021-01-30 01:29

    Alternative solution with pure javascript (jQuery). I'm working with MVC4 + Bootstrap3 but it works perfect for you.

    $(function () {
            $(".validation-summary-errors").addClass('alert alert-danger');
            $(".validation-summary-errors").prepend('<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>')
        });
    

    If you don't want to write server side logic then is a nice alternative solution.

    0 讨论(0)
  • 2021-01-30 01:30

    In MVC 5, ViewData.ModelState[""] always returned a null value. I had to resort to the IsValid command.

    if (!ViewData.ModelState.IsValid)
    {
       <div class="alert alert-danger">
          <a class="close" data-dismiss="alert">×</a>
          <strong>Validation Errors</strong>
          @Html.ValidationSummary()
       </div>
    }
    
    0 讨论(0)
提交回复
热议问题