8

I have the following string value generated in the controller.

 cc.lstchat = "Reply the Number with your question... <br />";

                foreach (clsChat item in lstchat)
                {

                    cc.lstchat =cc.lstchat + item.DisplayNumber+". " + item.ChatItem+" <br/> ";
                }

Now i'm trying to display above string value inside a div tag in my view but it does not render the line breaks.

<div id="divChat" style="width:400px;height:300px;border:double">
           @Model.lstchat.ToString()
       </div>

enter image description here

chamara
  • 12,649
  • 32
  • 134
  • 210
  • possible duplicate of [ASP.NET MVC Razor render without encoding](http://stackoverflow.com/questions/4071602/asp-net-mvc-razor-render-without-encoding) – Ramesh Rajendran Oct 25 '13 at 04:44

3 Answers3

15

Try the @Html.Raw method

@Html.Raw(Model.lstchat)
Dave Zych
  • 21,581
  • 7
  • 51
  • 66
4

Use Html.Raw() it is used to render any string as HTML.

`@Html.Raw("input data in here is rendered as HTML only")`
2

please please be careful with @Html.Raw() because of HTML injection (eg my comment will be <script>...</script> test - that an XSS right away. If you just want line breaks - consider this answer:

@Html.Raw(Html.Encode(Model.CommentText).Replace("\n", "<br />"))
avs099
  • 10,937
  • 6
  • 60
  • 110