Embarrassingly newbie question:
I have a string
field in my model that contains line breaks.
@Html.DisplayFor(x => x.MultiLineText)
A HtmlHelper extension method to display string values with line breaks:
public static MvcHtmlString DisplayWithBreaksFor(this HtmlHelper html, Expression> expression)
{
var metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
var model = html.Encode(metadata.Model).Replace("\r\n", "
\r\n");
if (String.IsNullOrEmpty(model))
return MvcHtmlString.Empty;
return MvcHtmlString.Create(model);
}
And then you can use the following syntax:
@Html.DisplayWithBreaksFor(m => m.MultiLineField)