Santiago
Santiago

Reputation: 2330

Can I write an inline if with HTML content?

I want to write something like:

@( checkCondition ? "<span class='label'>Right!</span>" : "")

But it is showing the source code instead the HTML, there is a easy way to do this?

Thank you!

Upvotes: 27

Views: 40509

Answers (3)

A. Morel
A. Morel

Reputation: 10384

We can also do like that:

@if (checkCondition ) { <text><span class='label'>Right!</span></text> }

The text tag allows you to write html with syntax highlighting!

Upvotes: 3

Mish Ochu
Mish Ochu

Reputation: 466

You can be even more concise (granted harder to read) with this:

@Html.Raw(checkCondition ? "<span class='label'>Right!</span>": string.Empty)

Upvotes: 27

Volodymyr Machula
Volodymyr Machula

Reputation: 1604

You can use @Html.Raw(mystring) method like this:

@( checkCondition ? Html.Raw("<span class='label'>Right!</span>") : Html.Raw(""))

Upvotes: 62

Related Questions