1
<%= Html.TextArea("text", "This is <b>Zeb</b>") %>

From the above statement what i expect is to show the string in textbox as:


This is Zeb.

But what i actually get is

This is <b> Zeb </b>

My string is not properly encoded. So please can someone tell me where is the problem?

enter image description here

I have read it from book Professional ASP.NET MVC 4. It's on page 100.

6
  • Encode to what standard? Why would ASP.NET know you want to convert HTML <b> tags to asterisks? Commented Dec 24, 2012 at 9:54
  • Sorry i edited the code that was my mistake. Now check it once again... Commented Dec 24, 2012 at 9:57
  • 1
    Okay, then the answer is because HTML is not rendered in a textarea, it shows a literal string. Commented Dec 24, 2012 at 9:58
  • I don't think you can do this on a normal textarea. You need a rich textbox or something. just like email editors. Commented Dec 24, 2012 at 9:59
  • 1
    with the standard textarea its not possible to show the formatted text in the textarea control, take a look at tinymce.com this rich text editor has been used in the rpoject on which you are working, ask your ATL (assistant team lead ;) ) Commented Dec 26, 2012 at 5:13

2 Answers 2

3

There is no problem, it is expected behavior. Here is a same question: ASP.NET MVC3, Html.TextAreaFor without encoding?

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

Comments

0

Your question has nothing to do with encoding. You are telling MVC (and the browser) to show a text area to edit that string, and that's what it's doing.

If you want an HTML editor with which your users can edit the HTML (while seeing it properly rendered), you should check this post: What's the best WYSIWYG editor when using the ASP.NET MVC Framework?

Basically, you'll usually have to include the .js and .css of the selected editor, and activate it on the text area that you are currently sending to the broser. That will create an editor like the one you'll find in Wordpress, for example.


Edit:

Now that you have edited your question, I think you are misunderstanding what the book was referring to. When it says that your text has been encoded it means that the < > characters have been translated into & lt; and & gt; so that the browser doesn't treat them as "open HTML tag" and "close tag".

If you look at the source HTML in your browser, you'll see that the string has been properly encoded (as & lt; and & gt;).

2 Comments

Really? What about contenteditable? jsfiddle.net/mekwall/XNkDx No files necessary.
What does that have to do with my answer? I didn't say anything about not being able to do it without extra files, I just pointed to what's probably the easiest solution for him. The last paragraph was meant to be a little indication as to what to do once he installs TinyMCE or any other of the editors mentioned in that post.

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.