Pregunta

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?

¿Fue útil?

Solución

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

Otros consejos

Try this approach:

@if(active=="test")
{
<a href='?log' id='button'>TEST</a>
}
else
{
<a href='?test' id='button'>TEST</a>
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top