Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically show bootstrap's surrounding error div on ValidationSummary?

I have a ASP .NET MVC 4.5 that uses Razor View Engine. I also added Bootstrap to it.

My doubt is: How do I dynamically show or hide a div according to the @Html.ValidationSummary()? - preferably without JQuery.

In my specific case, there's a form which contains several inputs. Whenever a validation error occours (eg. empty field), I intend to show the associated error message in a single div.

<div class="alert alert-danger">
@Html.ValidationSummary();
</div>

Thanks in advance.

like image 695
Guilherme Calegari Avatar asked Dec 05 '13 11:12

Guilherme Calegari


1 Answers

This is all built into MVC no reason for any sort of hacking.

Simply use:

@Html.ValidationSummary(false, "", new { @class = "alert alert-danger" })

Assitionally some CSS to hide it when there are no errors:

.validation-summary-valid.alert-danger {
    display: none;
}

Much easier, much cleaner.

like image 172
shenku Avatar answered Oct 04 '22 03:10

shenku