Reputation: 15219
I have the following sample code in a razor view on a MVC4 sample project:
<p>@((@Model == null) ? "<unknow man>" : @Model.ClientName)</p>
<p>
@if (@Model == null)
{
@Html.Encode("<unknow man>")
}
else
{
@Model.ClientName;
}
</p>
The output is a little bit odd for me...
In Internet Explorer I see:
<p><unknow man></p>
<p>&lt;unknow man&gt;</p>
In Chrome:
<p><unknow man></p>
<p><unknow man></p>
And what is the most incredible (for me) is that finally I have the same visual output:
<unknow man>
<unknow man>
I wanted to display, however, this one in HTML:
<p><unknow man></p>
<p><unknow man></p>
and this one to the user:
<unknow man>
<unknow man>
I found a solution, finally, to display properly the string, like this
@if (@Model == null)
{
@("<unknow man>")
}
But who can explain me the difference for the HTML.Encode in browsers, and why this didn't work like coded in the first example?
Upvotes: 0
Views: 5785
Reputation: 24832
<p>@((@Model == null) ? @Html.Raw("<unknow man>") : @Model.ClientName)</p>
<p>
@if (@Model == null)
{
<text><unknow man></text>
}
else
{
@Model.ClientName;
}
</p>
Upvotes: 0