0

I use ASP.NET MVC in my project. I have used Html.Encode to display some text content and while editing i am finding the text and displaying it in the edit textbox.

Here is my issue, if the text contains some special character, say , while editing its displayed as <hello> but i wanna display the same as

How can i do this?

3 Answers 3

1

You need to Html.Decode it before displaying it to user.

Javascript does not decode html natively

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

Comments

0
<%= Html.TextAreaFor(model=>model.PropertyName) %>

<textarea>
<%= your_variable %>
</textarea>

same can be done for input.type=text

OR

whatever the way u r doing, there is no need to do Html.Encode when displaying ur data in text box/textarea. Html.Encoding is basically done to avoid injecting script that malicious user tries to inject into ur page. But there is no such issue in case of textarea/input.type=text

5 Comments

if i use <% =Model.PropertyName%>, its not displaying the data which contains special character...like <Hello>
remember when u want to display data to div, like inside a div, you have to do Html.Encode. while when you want to display data inside textarea/input.type=text. Just skip the part Html.Encode, i think that would run ur code smoothly.
I am displaying the content in a span using Html.Encode, but when i get the same and display it in the textbox using javascript i am getting &lt;hello&gt;
oh!!, so are are you first showing text inside span and then using javascript to show that text inside a textbox??
then before placing you html into textbox, with javascript you have to encode it, read this; strictly-software.com/htmlencode
0

The Html.Encode() doesn't support an option to prevent double encoding HTML entities like the PHP htmlentities() function. You'd need to either decode the string first or write a custom HTML Helper that doesn't double encode characters.

Comments

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.