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>
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.
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.
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.
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 .
Try the @Html.Raw
method
@Html.Raw(Model.lstchat)
Use Html.Raw()
it is used to render any string as HTML.
`@Html.Raw("input data in here is rendered as HTML only")`
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 />"))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With