Question

I've been using DisplayTemplates/EditorTemplates for relatively simple types, like DateTime and DateTime?, etc. Now I want to use it to render a more complex type that is handing off my view model. The complex type is basically an entity from EF.

The view model looks like this:

public class RentalViewModel
{
    public Inventory InventoryItem { get; set; }
}

I created a template inside of Shared/DisplayTemplates named "Inventory.cshtml". It looks like this (I've removed a bit, but it's essentially the same)

@model MyCompany.MyApp.BusinessObjects.Models.Inventory
<div class="highlightBox">
   <ul class="double">
      <li>Dimensions:</li>
      <li>@Html.DisplayTextFor(i => i.Dimensions)</li>
      <li>Capacity</li>
      <li>@Html.DisplayTextFor(i => i.Capacity)</li>
   </ul>
</div>

Inside of a view, I have this:

@Html.DisplayTextFor(i => i.InventoryItem)

I was expecting it to resolve my DisplayTemplate here and use it. However, it doesn't. I added a UIHint property to my view model, just in case:

public class RentalViewModel
{
    [UIHint("Inventory")]
    public Inventory InventoryItem { get; set; }
}

That didn't seem to make a difference. I'm obviously missing something here, but I'm not seeing it.

When in renders, all I get is the number "1" inside of the HTML - that's the primary key for the entity I'm rendering.

Any ideas? This kind of thing should work, right?

EDIT: Just changing from @Html.DisplayForText(i => i.InventoryItem) to @Html.DisplayFor(i => i.InventoryItem) fixes the problem. No UIHint attribute is needed.

Was it helpful?

Solution

Instead of:

@Html.DisplayTextFor(i => i.InventoryItem)

try:

@Html.DisplayFor(i => i.InventoryItem, "Inventory")

OTHER TIPS

DisplayTextFor is not a template rendering function. Only DisplayFor, EditorFor, and DisplayForModel and EditorForModel will look at and use your templates.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top