nikolaia
nikolaia

Reputation: 767

If statement inside div tag with Razor MVC3

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

Answers (3)

balexandre
balexandre

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

Zruty
Zruty

Reputation: 8667

<div class='eventDay @(e.Value.Count<1?"noEvents":"")'>

Upvotes: 65

Goran Obradovic
Goran Obradovic

Reputation: 9051

Try

@{
var css = "eventDay";
if(e.Value.Count < 1){
 css += " noEvents";
}
}
    <div class="@css">

Upvotes: 4

Related Questions