0

Is there a reason /Views/Shared/DisplayTemplates/Object.cshtml is not being picked up by views when calling @Html.DisplayFor(x => x.Date)?

If I name the file DateTime.cshtml it is picked up by DateTime properties as is, but I don't want to have to create a template for each type. Shouldn't Object.cshtml just be used for everything?

I have not decorated the properties with any attributes.

The view's code:

@model Object

<div>
    <strong>@Model</strong>
</div>
goodmanship
  • 334
  • 1
  • 13
mxmissile
  • 11,464
  • 3
  • 53
  • 79

1 Answers1

0

DisplayTemplates are not intended to use like this. You should create a partial view with this content:

@model Object

<div>
    <strong>@Model</strong>
</div>

And instead of this: @Html.DisplayFor(x => x.Date)

you write this: @Html.Partial("YourPartialView", Model.Date)

With this you achieve the result you want. A DisplayTemplate is a more specific thing.

Peter Porfy
  • 8,921
  • 3
  • 31
  • 41
  • Say I created template files for each type. Would it still be classified as "not intended"? I'm missing the point of them if I can't customize? – mxmissile Feb 26 '13 at 00:50
  • If you create the template files for each type explicitly then that would work but would be quite pointless – Peter Porfy Feb 26 '13 at 00:53
  • Here is an explanation of the differences of the concepts above: http://stackoverflow.com/a/5037631/238682 – Peter Porfy Feb 26 '13 at 00:55
  • Makes sense, but this leads to another question I have posted as a followup: http://stackoverflow.com/questions/15096530 – mxmissile Feb 26 '13 at 18:29