Reputation: 3665
What would be the best way to add/remove a css class to a div from the controller?
I have a div in a razor view like so (simplified)
<div id="divToToggle" class="hidden">
//content
</div>
In my controller, depending if there is content or not, I want to add or review that class="hidden".
if (model.name.HasAValue())
{
model.company = Method();
//Toggle div to visible here.
}
I have thought about making a new property in the model and setting that value and then in the razor view doing like class="@Model.CssName" However, I have numerous tabs to deal with and don't want to clutter the model up with another set of css properties. What would be the most efficient way to do this?
Upvotes: 1
Views: 4472
Reputation: 868
You can also add class, if you have array of class and you want to append in foreach loop
<div class="panel @arrayClass[count1] panel-stat">
</div>
If you have class returning from model
<div class="panel @model.class panel-stat">
<div>
Or you can also use if condition or ternary operator as per your requirement you can do what you want to achieve.
Upvotes: 0
Reputation: 11964
In view you can do:
<div id="divToToggle" class=@(model.name.HasAValue()?"hidden":"visible")>
//content
</div>
Upvotes: 4