MVC3 TextBoxFor with coded text

Is there a way to use the TextBoxFor coded text assistant?

for example: When using the following MVC3 helper with the Razor view engine:

@Html.TextBoxFor(model => model.Description) 

and the value of the model. The description is encoded, for example:

  <script>alert();'</script> 

the result is a text field with an encoded string, when the desired result is a text field with a decrypted string:

  <script>alert();'</script> 

Is there a way to use MVC TextBoxFor with an encoded string instead of using

 @Html.TextBox("Description", Server.HtmlDecode(Model.Description)) 

?

+7
source share
1 answer

You need to html decode your string.

Use System.Web.HttpUtility.HtmlDecode for this.

 System.Web.HttpUtility.HtmlDecode("&lt;script&gt;alert();&#39;&lt;/script&gt;") 

will result in

 <script>alert();'</script> 

TextBoxFor does not support this, you have 2 options

1. Decoding before display

  @{ Model.Description = System.Web.HttpUtility.HtmlDecode(Model.Description); } @Html.TextBoxFor(model => model.Description) 

2. Use @ Html.TextBox for this

  @Html.TextBox("Description", System.Web.HttpUtility.HtmlDecode(Model.Description)) 

hope this helps

+5
source

All Articles