How can I display HTML text that has line breaks and have those line breaks maintained?
public void OnGet()
{
ViewData["Msg"] = "Hello World \n Hello World \n Hello World \n Hello World";
}
The above is straight text with embedded line feeds
@page
@model IndexModel
@{
ViewData["Title"] = "Home page";
}
<textarea rows="4" asp-for="@ViewData["Msg"]" style="width:100%">ViewData["Msg"]</textarea>
Message : @ViewData["Msg"]
The text views correctly in the textarea but not as straight HTML Message : @ViewData["Msg"]
. I just want to display static text and not having to add it to a predefined textarea
. Below shows the result of the code below:
Try to replace "\n" by line break tag:
public void OnGet()
{
var text = " Hello World \n Hello World \n Hello World \n Hello World".Replace("\n","<br>");
ViewData["Msg"] = $"<div>{text}</div>";
}
On the view side use Html.Raw() helper to render without HTML-encoding:
@Html.Raw(@ViewData["Msg"])