Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Line breaks are not rendered in MVC Razor View

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

like image 277
chamara Avatar asked Oct 25 '13 03:10

chamara


People also ask

What is razor in MVC What are the main Razor syntax rules?

Razor is a simple programming syntax for embedding server code in web pages. Razor syntax is based on the ASP.NET framework, the part of the Microsoft.NET Framework that's specifically designed for creating web applications.

Does MVC use razor pages?

A Razor Page is almost the same as ASP.NET MVC's view component. It has basically the syntax and functionality same as MVC. The basic difference between Razor pages and MVC is that the model and controller code is also added within the Razor Page itself. You do not need to add code separately.

What is razor view in ASP NET MVC?

Razor is a markup syntax that lets you embed server-based code into web pages using C# and VB.Net. It is not a programming language. It is a server side markup language. Razor has no ties to ASP.NET MVC because Razor is a general-purpose templating engine. You can use it anywhere to generate output like HTML.

What is RenderSection in razor?

RenderSection(String) In layout pages, renders the content of the section named name . RenderSection(String, Boolean) In layout pages, renders the content of the section named name .


3 Answers

Try the @Html.Raw method

@Html.Raw(Model.lstchat)
like image 138
Dave Zych Avatar answered Oct 16 '22 22:10

Dave Zych


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

`@Html.Raw("input data in here is rendered as HTML only")`
like image 31
Vinay Pratap Singh Bhadauria Avatar answered Oct 16 '22 22:10

Vinay Pratap Singh Bhadauria


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 />"))
like image 30
avs099 Avatar answered Oct 16 '22 21:10

avs099