0

I'm working on ASP.NET Web Pages (Razor) project. I want to display HTML code if the condition is true but it's seems like the browser is showing it as Plain Text instead of HTML code.

Here is my HTML :

<li>@(active=="test" ? "<a href='?log' id='button'>TEST</a>" : "<a href='?test' id='button'>TEST</a>")</li>

I want if @(active=="test") condition is true then my HTML code change to another. Please help me to do this?

0

2 Answers 2

1

You can also try this

<li>@Html.Raw(active=="test" ?"<a href='?log' id='button'>TEST</a>" : "<a href='?test' id='button'>TEST</a>")</li>

The point is Razor always encodes output, you need to tell it not to do it using Html.Raw() helper

Sign up to request clarification or add additional context in comments.

Comments

1

Try this approach:

@if(active=="test")
{
<a href='?log' id='button'>TEST</a>
}
else
{
<a href='?test' id='button'>TEST</a>
}

5 Comments

Thanks It's working! But I want to know also how it's working now and what's wrong with my method?
Razor does not recognize the '?' operator so it just renders it as plain text. Razorview engine has its ups and downs.
Because browser source code showing the same result with both methods.
OKK... Thanks anyway.
@MaryMelody With this approach, Razor stops processing when it meets '<' and just outputs the tag. What you did was you gave it a string not a tag.

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.