Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"> 
like image 872
nikolaia Avatar asked Jun 15 '11 10:06

nikolaia


People also ask

How do you write if condition in razor view?

The If Condition The if statement returns true or false, based on your test: The if statement starts a code block. The condition is written inside parenthesis. The code inside the braces is executed if the test is true.

What is @model in razor?

The @ symbol is used in Razor initiate code, and tell the compiler where to start interpreting code, instead of just return the contents of the file as text. Using a single character for this separation, results in cleaner, compact code which is easier to read.

What is the Razor syntax in dotnet?

Razor is a markup syntax for embedding . NET based code into webpages. The Razor syntax consists of Razor markup, C#, and HTML.


1 Answers

<div class='eventDay @(e.Value.Count<1?"noEvents":"")'> 
like image 77
Zruty Avatar answered Sep 20 '22 20:09

Zruty