Nour
Nour

Reputation: 5439

MVC, make conditional output in simplest way

hi i have a variable called isEnglish

if it is true I want to output something like this:

<div orientation="left"> </div>

otherwise:

<div orientation="right"> </div>

the following code failed to compile :

<div orientation="<%=isEnglish?? %>left<%:%>right<% %>"> </div>

I know a way which is long, by using the (if) and Writer.Write method

is there another simple way ?

Upvotes: 0

Views: 151

Answers (3)

Milimetric
Milimetric

Reputation: 13549

The code you want is this:

<div class="<%= isEnglish ? "left" : "right" %>"></div>

But check out Razor if you're using MVC, much cleaner syntax.

Upvotes: 1

Justin Niessner
Justin Niessner

Reputation: 245389

You could use a conditional statement:

<div orientation="<%= isEnglish ? "left" : "right" %>"></div>

Or, preferably (to me at least), you would remove this logic from the View altogether and create a ViewModel. You can then put the logic in the mapping between the Model and the ViewModel.

That way you don't have spaghetti code in your View. It might look something like:

<div orientation="<%= Model.Orientation %>"></div>

Upvotes: 3

Craig Stuntz
Craig Stuntz

Reputation: 126547

<div orientation="<%= isEnglish? "left" : "right" %>"> </div>

Upvotes: 3

Related Questions