34
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, "<br/>"));
}
Use
<%= Html.MultiLineText(Model.MultilineText) %>
Or
@Html.MultiLineText(Model.MultilineText)