<%@ 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.

share|improve this question
1  
Please send the code in controller action. – Mehrdad Afshari May 18 '09 at 12:57
feedback

3 Answers

up vote 0 down vote accepted

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.

share|improve this answer
feedback

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

share|improve this answer
feedback

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!

share|improve this answer
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.