Reputation: 767
I'm trying to have an if statement inside a class property of a div tag using the Razor View Engine. How can i get this working and is there perhaps a better way to do this?
<div class="eventDay @if(e.Value.Count < 1){Html.Raw("noEvents");}">
If there are no events the CSS class noEvents should be added. Expected result:
<div class="eventDay noEvents">
Upvotes: 30
Views: 44742
Reputation: 75083
the Razor Way is using <text>
, you also learn more about the Razor syntax here and here:
<div class="eventDay @if(e.Value.Count < 1) { <text>noEvents</text> }">
Upvotes: 20
Reputation: 9051
Try
@{
var css = "eventDay";
if(e.Value.Count < 1){
css += " noEvents";
}
}
<div class="@css">
Upvotes: 4