user2687153
user2687153

Reputation: 447

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).

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;

Upvotes: 1

Views: 2021

Answers (1)

PaulBinder
PaulBinder

Reputation: 2052

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);
        }


    }

Upvotes: 2

Related Questions