43

I am new to MVC and know how to use Html.Displayfor(), but I don't know when to use it?

Any idea?

tereško
  • 58,060
  • 25
  • 98
  • 150
BreakHead
  • 10,480
  • 36
  • 112
  • 165

2 Answers2

75

The DisplayFor helper renders the corresponding display template for the given type. For example, you should use it with collection properties or if you wanted to somehow personalize this template. When used with a collection property, the corresponding template will automatically be rendered for each element of the collection.

Here's how it works:

@Html.DisplayFor(x => x.SomeProperty)

will render the default template for the given type. For example, if you have decorated your view model property with some formatting options, it will respect those options:

[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}")]
public DateTime SomeProperty { get; set; }

In your view, when you use the DisplayFor helper, it will render the property by taking into account this format whereas if you used simply @Model.SomeProperty, it wouldn't respect this custom format.

but don't know when to use it?

Always use it when you want to display a value from your view model. Always use:

@Html.DisplayFor(x => x.SomeProperty)

instead of:

@Model.SomeProperty
Lauren Rutledge
  • 1,195
  • 5
  • 18
  • 27
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Thanx Darin, with Display Template you mean rendering strong type 'View/PartialView'? – BreakHead Feb 27 '12 at 13:01
  • @BreakHead, yes, your views should always be strongly typed to a view model. – Darin Dimitrov Feb 27 '12 at 13:02
  • again thanks, so what if I have multiple views of same type? which one it will render? – BreakHead Feb 27 '12 at 13:05
  • @BreakHead, display templates work by convention. It will look for a view `~/Views/Shared/DisplayTemplates/TypeOfTheProperty.cshtml` unless you explicitly specify a different name as second argument for the `DisplayFor` template. Take a look at the Brad Wilson blog post that I have linked to in my answer: http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-3-default-templates.html – Darin Dimitrov Feb 27 '12 at 13:06
  • Ah! So to display a template it should be in a shared folder, and that view name should be same as of type, right? – BreakHead Feb 27 '12 at 13:08
  • @BreakHead, exactly. And if it is a collection property, then the display template name should be the type of the elements in the collection. For example if you have `IEnumerable` the display template should be `Foo.cshtml`. – Darin Dimitrov Feb 27 '12 at 13:13
  • 2
    Can we get an explanation why `@Model.SomeProperty` should be avoided? Consistency? – Drew Kennedy Sep 25 '15 at 18:20
  • Excellent explanation – Bhuwan Pandey Aug 01 '16 at 19:24
  • @DarinDimitrov this is so helpful. Mind if I ask when to use @Model.SomeProperty? – Prageeth Liyanage Dec 15 '22 at 07:41
7

I'm extending @Darin's answer.

Html.DisplayFor(model => model.SomeCollection) will iterate over items in SomeCollection and display the items using DisplayFor() recursively.

Zruty
  • 8,377
  • 1
  • 25
  • 31
  • 1
    I liked this except one thing: If I am printing a list of strings,

    @Html.DisplayFor(x => x.Messages)

    prints out strings ok but without carriage return.
    – bob Nov 28 '12 at 20:07
  • 1
    There is no such thing as carriage return in HTML, right? I think the default template for a string is '@model', so your strings will end up concatenated. If you want to wrap every string into a, say,
    , this approach won't work
    – Zruty Dec 03 '12 at 18:29
  • @Zruty how can I convert it to UpperCase? – SamR May 07 '14 at 04:07