I have a textarea
in mvc. When data is entered into that and I\'m displaying it back to the user, how do I show the line breaks?
I display like this:
This works for me -
<%= HttpUtility.HtmlDecode(Html.ActionLink("AOT <br/> Claim #", "actionName" ))%>
It doesn't look like you're trying to HTML Encode so a regular replace should work fine.
<%= Model.Description.Replace(Environment.NewLine, "<br />")%>
For a slightly different (and, IMHO, better and safer) way of solving the problem, see this answer to a similar question.
Basically, instead of going through the trouble of converting all new line characters to <br>
elements, it is as simple as applying the following CSS to the element where you are showing the inputted text:
white-space: pre-line
Try something like this:
<%=
Regex.Replace(
Html.Encode(Model.Description),
Environment.NewLine,
"<br/>",
RegexOptions.IgnoreCase||RegexOptions.Multiline
)
%>
For Asp.net mvc razor,i used Html.Encode for blocking xss attacks
@Html.Raw(Html.Encode(Model.Description).Replace(Environment.NewLine, "<br />"))
Maybe it's the answer: How do you handle line breaks in HTML Encoded MVC view?
or
you can try:
Model.Description.Replace(Environment.NewLine, "<br/>");