Hi, I am not able to figure out this simple thing in ASP.NET MVC:
I have two tables:
Customer:
CustomerID
FirstName
LastName
AddressID
AddressTemporaryID
Address:
AddressID
Street
City
I have my relations(FK) set in DB and trying to use Entity Framework. My navigation properties(FK) are named Addresses and Addresses1. I created repository:
public interface ICustomertRepository {
IQueryable<Customer> FindAllCustomers();
}
public class CustomerRepository : MvcApplication2.Models.ICustomerRepository {
public IQueryable<Customer> FindAllCustomers() {
return db.Customers;
}
}
And my HomeController has this:
public ActionResult Index()
{
var query = customerRepository.FindAllCustomers();
return View(query);
}
But how can I access values in my View? I can see item.Adresses.Street/item.Adresses1.Street with Intellisense in Model but when I will try it, I get NullReferenceException:
<% foreach (var item in Model)
{ %>
<tr>
<td>
<%= Html.Encode(item.FirstName) %>
</td>
<td>
<%= Html.Encode(item.Surname) %>
</td>
<td>
<%= Html.Encode(item.Adresses.Street) %>
</td>
<td>
<%= Html.Encode(item.Adresses1.Street) %>
</td>
</tr>
<% } %>
I am completely lost. I tried to search everywhere but without any success.