Inspired by DisplayTemplates for common DataTypes,
I override (introduce?) a default DisplayTemplate for DataType.MultilineText
, /Views/Shared/DisplayTemplates/MultilineText.cshtml
containing just this line:
<span style="white-space: pre-wrap">@this.Model</span>
(Of course you could replace this style, by a css-class, or replace newlines inside the view, if you prefer that.)
I guess this template is automatically resolved, because I had no need for UIHint
or any other reference or registration.
Using the DisplayTemplate instead of introducing a HtmlHelper
-method has the advantage, that it trickles down to properties and views that are not explicitly defined.
E.g. DisplayFor(MyClassWithMultilineProperties)
will now also correctly display MyClassWithMultilineProperties.MyMultilineTextProperty
, if the property was annotated with [DataType(DataType.MultilineText)]
.
in ASP.NET MVC Razor view](https://stackoverflow.com/questions/4220381/replace-line-break-characters-with-br-in-asp-net-mvc-razor-view) – avs099 Sep 28 '20 at 15:54