简体   繁体   中英

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">
<div class='eventDay @(e.Value.Count<1?"noEvents":"")'>

Razor Way正在使用<text> ,您还可以在此处此处进一步了解Razor语法:

<div class="eventDay @if(e.Value.Count < 1) { <text>noEvents</text> }">

Try

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM