Mike Flynn
Mike Flynn

Reputation: 24325

ASP.NET MVC User Control and Apostrophe being encoded

Why would this user control have the apostrophes encoded? It is causing issues with Jquery Templates. The apostrophe is being encoded to ' from an AJAX request.

    <script type="text/html" id="editTimeTemplate">
        @Html.TextBoxFor(q => q.Time, new { type = "text", maxlength = 8, data_bind = "value: editTime.time.time, time: { options : { defaultTime: '8:00', showPeriod: true, showLeadingZero: false} }" })
    </script>
    <script type="text/html" id="editTimeTemplate">
    <input data-bind="value: editTime.time.time, time: { options : { defaultTime: &#39;8:00&#39;, showPeriod: true, showLeadingZero: false} }" id="Time" maxlength="8" name="Time" type="text" value="" />
</script>

Upvotes: 2

Views: 804

Answers (1)

Mirko
Mirko

Reputation: 4282

Internally the TextBoxFor uses a TagBuilder, which will do this encoding for all attribute values. The simple workaround is to not use a helper method, but an HTML tag (I am using MVC4, so the IdFor and NameFor methods you would have to implement yourself).


<input type="text" id="@Html.IdFor(m => m.Time)" name="@Html.NameFor(m => m.Time)" maxlength="8" data-bind="value: editTime.time.time, time: { options : { defaultTime: '8:00', showPeriod: true, showLeadingZero: false} }" />

or of course just:


<input type="text" id="Time" name="Time" maxlength="8" data-bind="value: editTime.time.time, time: { options : { defaultTime: '8:00', showPeriod: true, showLeadingZero: false} }" />

If this bothers you a lot (or you do this a lot) I would go the route of implementing my own TexBoxFor extension method and override any behavior you do not like.

Upvotes: 4

Related Questions