Anonymous
Anonymous

Reputation: 10216

ASP.NET Web Pages (Razor) change html code if condition is true not working

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?

Upvotes: 0

Views: 1232

Answers (2)

MikeSW
MikeSW

Reputation: 16348

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

Upvotes: 1

leskovar
leskovar

Reputation: 661

Try this approach:

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

Upvotes: 1

Related Questions