4

I am trying to display a customer address in multiline which works for @Html.EditorFor but not @Html.DisplayFor. I have tried other posts on here but have not got it to work. Here is what have started with and what I have tried with no luck

@Html.DisplayFor(model => model.CustomerPostalAddress)
@Html.Raw(Html.Encode(Model.CustomerPostalAddress).Replace("\n","<br/>"))

Here is the field in the model

 [DataType(DataType.MultilineText)]
 public string CustomerPostalAddress { get; set; }

Any help would be much appreciated

WillNZ
  • 765
  • 5
  • 13
  • 38

3 Answers3

6

You would want to create a DisplayTemplate in Views/Shared/DisplayTemplates/DisplayPostalAddr.cshtml

@model string

<p>@Model</p>

then give you model a hint on how to display it

[DataType(DataType.MultilineText)]
[UIHint("DisplayPostalAddr")]
public string CustomerPostalAddress { get; set; }
kevin_fitz
  • 837
  • 6
  • 19
  • Thanks for your answer Kevin, I have not yet had the time to try to implement this yet so havent marked it as the answer. I will let you know how I get on – WillNZ Nov 11 '12 at 23:08
  • Tiny typo -should be

    @Model

    I.e. a capital 'M'. Probably obvious to most, but might fox a newbie.
    – StuartQ Apr 04 '14 at 15:48
  • Good Catch, @StuartQ. I have edited the answer to reflect what you mentioned. – kevin_fitz Apr 08 '14 at 19:16
  • 1
    The template is used at runtime as advertised. However, to actually display multiple lines, one should use replace

    @Model

    with something like

    @Model
    , as suggested in the answer by Chtiwi Malek. Anyway, what's nice about this approach is that it provides a central place to configure the display style, so one need not configure each display separately.
    – Carsten Führmann Nov 01 '16 at 18:20
6

just add this css style property to render multiline texts, this is better then cpu consuming server side strings manipulation like .replace :

.multiline
{
   white-space: pre-wrap;
}

then

<div class="multiline">
  my multiline
  text
</div>

this will render newlines like br elements.

try it here : https://refork.codicode.com/xaf4

Chtioui Malek
  • 11,197
  • 1
  • 72
  • 69
  • 3
    Unfortunately, this does mean you need to be careful with the whitespace you're using as formatting... it renders that newline between the div and "my multiline" as a newline, too. – neminem Jun 20 '14 at 23:32
1

As mentioned in make DisplayFor display line breaks, 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>

I guess this template is automatically resolved, because I had no need for UIHint or any other reference or registration. (Of course you could also introduce a css-class, or replace newlines inside the view, if you prefer that.)

Using the default DisplayTemplate will fix the line breaks for all properties annotated with the (type-safe, meaningful) [DataType(DataType.MultilineText)], instead of just the ones annotated with UIHint("DisplayPostalAddr").

Yahoo Serious
  • 3,728
  • 1
  • 33
  • 37