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.

Zeb-ur-Rehman
  • 1,121
  • 2
  • 21
  • 37
  • Encode to what standard? Why would ASP.NET know you want to convert HTML `` tags to asterisks? – Grant Thomas Dec 24 '12 at 09:54
  • Sorry i edited the code that was my mistake. Now check it once again... – Zeb-ur-Rehman Dec 24 '12 at 09:57
  • 1
    Okay, then the answer is because HTML is not rendered in a `textarea`, it shows a literal string. – Grant Thomas Dec 24 '12 at 09:58
  • I don't think you can do this on a normal textarea. You need a rich textbox or something. just like email editors. – Kaf Dec 24 '12 at 09:59
  • So what is the problem exactly? That you see tags? It works as intended.. – Arcturus Dec 24 '12 at 10:41
  • 1
    with the standard textarea its not possible to show the formatted text in the textarea control, take a look at http://www.tinymce.com/ this rich text editor has been used in the rpoject on which you are working, ask your ATL (assistant team lead `;)` ) – Dakait Dec 26 '12 at 05:13

2 Answers2

3

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

Community
  • 1
  • 1
Dima
  • 6,721
  • 4
  • 24
  • 43
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;).

Community
  • 1
  • 1
salgiza
  • 5,832
  • 2
  • 26
  • 32
  • 3
    Really? What about `contenteditable`? http://jsfiddle.net/mekwall/XNkDx/ No files necessary. – Grant Thomas Dec 24 '12 at 10:03
  • 1
    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. – salgiza Dec 24 '12 at 10:09