BrokenCode
BrokenCode

Reputation: 961

MVC + Razor: how to add starting <div> conditionally?

I want to amend a div based on whether a variable is set or not.

So I would like to do something like this:

@if (SomethingIsSet) {

<div style="background:red">

} else {

<div style="background:blue"> }

But I get the following error message in Visual Studio:

The div element was not closed. All elements must be either self-closing or have a matchig end tag.

My div element is closed later on in the page.

Upvotes: 2

Views: 1255

Answers (1)

VDWWD
VDWWD

Reputation: 35514

You can use a ternary operator in Razor.

<div style="@(SomethingIsSet ? "background:red" : "background:blue")">

Upvotes: 7

Related Questions