27

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!

1

3 Answers 3

62

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

@( checkCondition ? Html.Raw("<span class='label'>Right!</span>") : Html.Raw(""))
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, I tried but I get error implicit conversion between'System.Web.IHtmlString' and 'string', seems that I have to use the standar If for this case.
Use Raw method for both strings as above. I have edited my answer.
27

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

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

Comments

3

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.