Search code examples
asp.netasp.net-mvc-3non-ascii-charactershtml.textboxfor

ASP.NET MVC3 - Display accented string in TextBoxFor


I'm working on an ASP.NET MVC3 project.

In a basic update page, I'm trying to display data recorded in the DB into some TextBoxFor. However, the data can contain some special characters, like quote ' or accented letters like é

When I write

@Html.TextBoxFor(m => m.MyProperty)

The displayed text looks like L'avée instead of L'avée.

I've seen this question but the answer doesn't change anything for me.

Is there a way to display my string PROPERLY with accented letters and quotes in a TextBoxFor ?

UPDATE

This field is in a partial view containing only this field :

@model MyApp.Models.SomeModel

<div class="form-group">
    @Html.LabelFor(m => m.MyModel.Submodel.MyProperty)
    @Html.TextBoxFor(m => m.MyModel.Submodel.MyProperty, new { @class = "form-control", @id = "txtMyProperty" })*@
</div>

Here, SomeModel is correctly displayed (believe me).

  • SomeModel has a MyModel property.
  • MyModel has a SomeModel property
  • SomeModel has a MyField field.

Everything is correctly filled from DB (believe me, it has been tested and re-tested). However, I can't correctly display MyField if it has special characters. It is displayed, but with HTML reprensentation like &#39;


Solution

  • The link he provided in the comments is the correct answer.

    @Html.TextBox("test", HttpUtility.HtmlDecode(Model.MyProperty))
    

    works correctly.

    My view

    @model MvcApplication1.Models.SomeModel
    
    @{
        ViewBag.Title = "title";
    }
    <h2>title</h2>
    @{ Html.RenderPartial("Partial"); }
    

    My Partial View

    @model MvcApplication1.Models.SomeModel
    
    <div class="form-group">
         @{
             Model.MyProperty = System.Web.HttpUtility.HtmlDecode(Model.MyProperty);
         }
        @Html.LabelFor(m => m.MyProperty)
        @Html.TextBoxFor(m => m.MyProperty, new { @class = "form-control", @id = "txtMyProperty" })
    </div>
    

    My controller

       public class HomeController : Controller
        {
            public ActionResult Index()
            {
                SomeModel model = new SomeModel
                    {
                        MyProperty = "L&#39;av&#233;e"
                    };
                return View(model);
            }
    
    
        }