2

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&#225;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 &#225;

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?

3
  • 3
    Whats is the value of model.Name. <span title="@Html.Raw("M&#225;rcia")">@Html.Raw("M&#225;rcia")</span> worked for me Commented Mar 21, 2013 at 13:27
  • 1
    Also provide some view code Commented Mar 21, 2013 at 13:29
  • Try using @HttpUtility.HtmlDecode(Html.DisplayFor(model => model.Name).ToString()) and let me know the result. Commented Jun 21, 2018 at 16:05

2 Answers 2

8

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&#225;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 &#225; 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>

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

2 Comments

Hello, its work for me too, because the second property its outside of html tag. Please try put the Model.Name2 into html attribute like <span title = "@Model.Name2" /> . The error persist
@jmrnet I have a Viewbag with document list and one of the document have the name "test1>test2". My html looks like @Html.DropDownList("Documents", (SelectList)ViewBag.Documents, "--Select Document--") When it runs in the dropdown list "test1>test2" shown as test2&gt;test1. What should i do?
0

You can put attribute name together with value.

@{  
    var title = "title='Márcia'";  
}  

<span @Html.Raw(title)>Márcia</span> 

Comments

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.