1

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;

6
  • 1
    stackoverflow.com/questions/15246439/… Commented Jun 27, 2014 at 13:30
  • I already see this answer, but I can't do @Html.TextBoxFor(HttpUtility.HtmlDecode(m => m.MyProperty)) Commented Jun 27, 2014 at 13:33
  • 1
    what is wrong with the 1st option in here?stackoverflow.com/a/8402310/2436549 Commented Jun 27, 2014 at 13:41
  • The problem is the field is still displayed as L&#39;av&#233;e. However when I put a breakpoint on it, the string is correct and contains my accented letters. But when the page is displayed, the field contains the HTML characters, which is not good. Commented Jun 27, 2014 at 13:45
  • I made a test application and provided my source below. It works perfectly. Upvodting Zsfars comment as it is correct. There is perhaps more information that we are not aware of...but with the information provided it answers the question. Commented Jun 27, 2014 at 13:50

1 Answer 1

2

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


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

7 Comments

In my case, I have a NullReferenceException on the @Html.TextBox line because Model is null. This field is in a partial view strongly typed with @model MyModel on line 1, can it be a problem ?
It's hard to say without seeing your full code. But if the partial view does not have a populated model then with or without encoding your data should not display.
The model is correctly populated, it works perfectly except if this field has accented characters. If it hasn't, the data is correctly displayed. The code populating this model is basic, I just get my DB object and fill my model with it. I'll investigate deeper because the problem must come from my app
Update your bug with the view code as you are stating it is populating a partial view.
can you also include your controller and parts of the view that call the partial
|

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.