Gaurav Arora
Gaurav Arora

Reputation: 2324

How to show validation summary per condition in MVC3 Razor View?

I am not sure whether its a good practice or not but there is a need to display Validation summary conditionally. Actually, I need to display validation summary under Yellow Square so, I created a CSS class for the same and trying to do this :

@if (!Html.ViewData.ModelState.IsValid)
{
   <p>
      <span class="message-wrapper warning">
         @Html.ValidationSummary(true)
      </span>
   </p>                                           
}
else
{ 
    @Html.ValidationSummary()
}

The problem with above is, the Yellow square is visible always which not to ?
The Yellow square should be display only when there are Validation errors and these errors should be displayed within the 'Yellow Square.

I am looking for the solution. Any help in this regard much appreciated!

Upvotes: 0

Views: 6241

Answers (1)

Adam Tuliper
Adam Tuliper

Reputation: 30152

Ideally you just can change the css class for this

.validation-summary-errors {
    background-color: #D9FFB2;
    border:1px solid #5CBA30;
    width: 400px;
    }

There are several covered methods here, so I won't repeat them all : )

Surrounding a ValidationSummary with a box via CSS

Upvotes: 2

Related Questions