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:
I like using HtmlExtensions, Environment.NewLine and Regex, which were in different answers, so I kinda put the above answers into a single method.
public static MvcHtmlString MultiLineText(this HtmlHelper htmlHelper, string text) {
if (string.IsNullOrEmpty(text)) {
return MvcHtmlString.Create(string.Empty);
}
return MvcHtmlString.Create(Regex.Replace(HttpUtility.HtmlEncode(text), Environment.NewLine, "
"));
}
Use
<%= Html.MultiLineText(Model.MultilineText) %>
Or
@Html.MultiLineText(Model.MultilineText)