开发者

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.H开发者_JAVA技巧elpText = 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+"


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)


Use this syntax for LabelFor

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜