0

I have some troubles with a text box input control value on an ascx page. It's value is somehow always html encoded, and I don't know how it can be disabled. For example when the value contains a < character it is always converted to &lt;. The strange thing is, it only happens on fields like Name.Lastname (which have a child property). My first thought was it could be caused by the Html extension method

Html.TextBoxFor(m => m.Name.LastName, new { maxlength = "100" }) 

but this is not the case, because when I use the html input directly, it's value is still encoded:

<input id="Name_LastName" maxlength="100" 
       name="Name.LastName" 
       type="text" value="<%= Model.Name.LastName %>" />

Does somebody know how the html encoding of text box values for fields like Name.LastName (with a child property) can be disabled?

Patrick Koorevaar
  • 1,219
  • 15
  • 24
  • possible duplicate of [A potentially dangerous Request.Form value was detected from the client](http://stackoverflow.com/questions/81991/a-potentially-dangerous-request-form-value-was-detected-from-the-client) – Eduardo Molteni May 08 '13 at 14:10
  • Check out this question: http://stackoverflow.com/questions/7136944/automatically-htmlencode-in-asp-net – DOK May 08 '13 at 14:11
  • shouldn't you be using `<%:`? – Jodrell May 08 '13 at 14:20
  • 2
    you can use also Html.Raw(Model.Name.LastName) – maxlego May 08 '13 at 14:22
  • The strange thing is that I also have a hidden input field which is also initialized with `Model.Name.LastName` and there it is not encoded and even when I use `<%= Model.Name.LastName %>` (not within an input field) I see the normal '<' character. – Patrick Koorevaar May 08 '13 at 16:08

1 Answers1

0

After more research I found out that it was caused by a javascript function which variables were initialized using the <%:, and this function was used to initialize the text boxes. So in the end it had nothing to do with child properties. I changed the <%: with <%= in the javascript part:

var lastName = "<%: Model.Name.LastName %>";

in

var lastName = "<%= Model.Name.LastName %>";

Patrick Koorevaar
  • 1,219
  • 15
  • 24