2

Ok, so I have a Course entity which has a many to one or zero relationship with the Industry entity. This means that a course can have a industry specified or not, for classification purposes. I created all entities and models through EF, so the only link stored in the Course entity is IndustryId.

The default views created upon creating the Controller displays the IndustryId.

What I'm trying to do now is to display the Description instead, which is a property of the Industry entity. Here is my code:

@foreach (var item in Model)
{
    <tr>
        <td><a href="@Url.Action("Details","Course", new { id=item.Id })"><img class="media-object" data-src="holder.js/64x64" /></a></td>
        <td>@Html.DisplayFor(modelItem => item.Name)</td>
        <td>@Html.DisplayFor(modelItem => item.Description)</td>
        <td>@Html.DisplayFor(modelItem => item.Industry.Description)</td>
        <td>@Html.DisplayFor(modelItem => item.Author)</td>
        <td>
            <a href="@Url.Action("Edit","Course", new { id=item.Id })"><i class="icon-edit"></i> Modify</a>
            <a href="@Url.Action("Delete","Course", new { id=item.Id })"><i class="icon-trash"></i> Delete</a>
        </td>
    </tr>
}

Problem now is that the line @Html.DisplayFor(modelItem => item.Industry.Description displays nothing. Does anyone know why?

AnimaSola
  • 7,146
  • 14
  • 43
  • 62
  • is lazy loading turned on and the entity is set up so that lazy loading can work (i.e. the type is not sealed and the navigation property is virtual?) – Pawel Mar 08 '13 at 18:18

1 Answers1

3

The most likely cause is that the Industry object is not being populated in your query, and therefore is null. You probably need add Include("Industry") to your populating LINQ query in your Model or Controller to eager-load that related entity.

Dave Swersky
  • 34,502
  • 9
  • 78
  • 118
  • 1
    @Dave, though I would use the type-safe variant of `Include` myself. – Kirk Woll Mar 08 '13 at 18:24
  • @KirkWoll If you're using LinqToSql you could use the type-safe variant, however LinqToEntities does not support that and uses Include with a string variable. – Silvermind Mar 08 '13 at 18:28
  • 1
    @Silvermind, that would not throw a null pointer because `DisplayFor` uses expression trees. Furthermore, entity framework does support a [type-safe `Include`](http://msdn.microsoft.com/en-us/library/gg671236(v=vs.103).aspx) whereas L2S does not even have that concept. – Kirk Woll Mar 08 '13 at 18:31
  • @KirkWoll You're right, right and right so +1. Didn't know that they added the support for type-safe Iclude in EF5. Btw L2S supports it through [DataLoadOptions](http://msdn.microsoft.com/en-us/library/system.data.linq.dataloadoptions.loadwith.aspx) – Silvermind Mar 08 '13 at 18:37
  • @Silvermind, L2S supports the *concept* (via [DataLoadOptions](http://msdn.microsoft.com/en-us/library/system.data.linq.dataloadoptions.aspx).[LoadWith](http://msdn.microsoft.com/en-us/library/bb534268.aspx)), but it's done in a fairly different way since it isn't composed in the query itself. – Kirk Woll Mar 08 '13 at 18:45
  • @Dave thanks! That supposedly fixed it but I'm having another issue with Entity Framework. – AnimaSola Mar 08 '13 at 18:48
  • @Dave btw, how do I handle Grand Child entities? (Course->Module->Chapter) Or should they be handled separately in the controllers of the Child entities? Thanks! – AnimaSola Mar 08 '13 at 18:49
  • @AnimaSola you can chain Include() calls within the LINQ query to include as many related entities as are relevant to the function that query supports. http://stackoverflow.com/questions/10822656/entity-framework-include-multiple-levels-of-properties – Dave Swersky Mar 08 '13 at 19:18