Embarrassingly newbie question:
I have a string field in my model that contains line breaks.
@Html.DisplayFor(x => x.MultiLineText)
Here's another extension method option.
public static IHtmlString DisplayFormattedFor(this HtmlHelper htmlHelper, Expression> expression)
{
string value = Convert.ToString(ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData).Model);
if (string.IsNullOrWhiteSpace(value))
{
return MvcHtmlString.Empty;
}
value = string.Join("
", value.Split(new[] { Environment.NewLine }, StringSplitOptions.None).Select(HttpUtility.HtmlEncode));
return new HtmlString(value);
}