views:

370

answers:

3
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/SiteControl.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<Website.SimpleRepository.Page>>" %>

<asp:Content ID="Content2" ContentPlaceHolderID="content" runat="server">

<h2>Page List</h2>

  <p>
    <%= Html.ActionLink("Create New", "Create") %>
</p>

<table>
    <tr>
        <th></th>
        <th>
            Display As
        </th>
        <th>
            Layout
        </th>

    </tr>

<% foreach (var item in Model) { %>

    <tr>
        <td>
            <%= Html.Encode(item.DisplayAs) %>
        </td>
        <td>
            <%= Html.Encode(item.Types.Name) %>
        </td>
    </tr>

<% } %>

</table>
</asp:Content>

I'm having difficult accessing the item.Types.Name it returns a null exception. I have my entity data model in a separate class library. Can anyone explain why its returning a null exception and what i can do to fix this? I have seen other projects that reference related table values but cannot work out why my work differs...

Any information on this subject would be great! Thanks.

A: 

Make sure your foreign key relations in your entity mapping allow for lazy loading or these reference properties.

Nick Berardi
A: 

How are you querying the data?

I've found that the current EF setup requires me to be more explicit with magic strings than I'd like:

public IEnumerable<Item> GetItems(){
  ObjectQuery<Item> items = ItemSet.Include("Types");

  return (from i in items
          where [...]
          select i);
}

This of course doesn't give you compile time checking of the foriegn keys, but it will at least return them now.

Zhaph - Ben Duguid
A: 

Turns out i'm a total newbie as i expected. The solution to my problem was i wasn't passing associated tables to the view. Thanks for your answers!