Reputation: 2180
I´m trying to write the word Márcia in html using MVC4 / Razor on view but the result is wrong like:
<span title="Márcia">Márcia</span>
Note: the word inside the tag SPAN print output correctly, but the word in same property of model (@Model.Name) display with the character encoded á
Already try all commands bellow, but anyone work when apply inside html attribute:
@Html.Raw(model.Name)
@Html.Raw(HttpUtility.HtmlDecode(model.Name))
@Html.Raw(Server.HtmlDecode(model.Name))
@HttpUtility.HtmlDecode(model.Name)
@Server.HtmlDecode(model.Name)
Why the html decode don't work when used inside a html attribute? How to make it work?
Upvotes: 2
Views: 6946
Reputation: 3459
You can put attribute name together with value.
@{
var title = "title='Márcia'";
}
<span @Html.Raw(title)>Márcia</span>
Upvotes: 0
Reputation: 548
This is what worked for me. Here is my HomeController
code:
public class HomeController : Controller
{
public ActionResult Index()
{
var model = new HomeModel();
model.Name1 = "Márcia";
model.Name2 = "Márcia";
return View(model);
}
}
Here is my Home Index view:
@model mvctestappweb.Models.HomeModel
@{
ViewBag.Title = "Index";
}
@Model.Name1
<br />
@Html.Raw(Model.Name2)
The output on the view looks like this:
Márcia
Márcia
EDIT: I see. The issue is that as soon as you put the RAW into the HTML <span>
tag that it converts the á to á
no matter what. I played with this a good bit and was unable to find a way to stop this from happening. So, here are two work arounds that might work for you:
(I know this isnt ideal, but it was the best work around I could find.)
My controller code:
public class HomeController : Controller
{
public ActionResult Index()
{
var model = new HomeModel();
model.Name = "Márcia";
model.SpanHtml = "<span title='Márcia'>Márcia</span>";
return View(model);
}
}
Here my view:
@model mvctestappweb.Models.HomeModel
@{
ViewBag.Title = "Index";
}
1: @Html.Raw("<span title=\"" + @Html.Raw(Model.Name) + "\">" + @Model.Name + "</span>")
<br />
2: @Html.Raw(@Model.SpanHtml)
The output on the view looks like this:
1: Márcia
2: Márcia
My HTML source looks like this:
1: <span title="Márcia">Márcia</span>
<br />
2: <span title='Márcia'>Márcia</span>
Upvotes: 8