Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 640
Santiago Avatar asked Sep 07 '25 13:09

Santiago


2 Answers

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

@( checkCondition ? Html.Raw("<span class='label'>Right!</span>") : Html.Raw(""))
like image 79
Volodymyr Machula Avatar answered Sep 09 '25 18:09

Volodymyr Machula


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

@Html.Raw(checkCondition ? "<span class='label'>Right!</span>": string.Empty)
like image 27
Mish Ochu Avatar answered Sep 09 '25 20:09

Mish Ochu