Nanek
Nanek

Reputation: 115

How to show a new line (<br>) with LabelFor?

I have a TextArea where a user can write some text. When i try to show the text with LabelFor, i get an "illegal characters" error, because it the string has "\r\n" for every new line.

I've tried to use this solution:

Show new lines from text area in ASP.NET MVC

and

     if (q.help_text != null)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            System.IO.StringReader sr = new System.IO.StringReader(q.help_text);

            string tmpS = null;
            do
            {
                tmpS = sr.ReadLine();
                if (tmpS != null)
                {
                    sb.Append(tmpS);
                    sb.Append("<br />");
                }
            } while (tmpS != null);
            var convertedString = sb.ToString();
            qvm.HelpText = convertedString;
        }
        else
            qvm.HelpText = q.help_text;

Instead of making new lines, LabelFor outputs the br code as well. How can i solve this?

EDIT

The solution was to do it this way:

@Html.Raw("

"+question.HelpText+"

Upvotes: 1

Views: 4156

Answers (2)

Govind Malviya
Govind Malviya

Reputation: 13743

Use this syntax for LabelFor

@Html.Raw(Html.LabelFor(x => x.Name))

Upvotes: 0

Ankur
Ankur

Reputation: 33637

This is because LabelFor is HTML encoding the text . This is done to avoid cross site scripting issues. What you can do is use pre tag to render the text area string as it is (with \r\n)

Upvotes: 3

Related Questions