I can display a list of all customers and I can display a list of all orders and I would like to take this further.
I would like to render two partial views on the same page to show customer details and orders relating to that customer.
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage< MyDemo.Models.CustomerAndOrdersViewModel >" %>
<% Html.RenderPartial("CustomerDetails", this.ViewData.Model.Customer); %>
<% Html.RenderPartial("CustomerOrders", this.ViewData.Model.Order); %>
I have created a viewmodel
public class CustomerAndOrdersViewModel
{
public CustomerAndOrdersViewModel(Customer customer,
IEnumerable<Order> orders)
{
this.Customer = customer;
this.Order = orders;
}
public Customer Customer { get; set; }
public IEnumerable<Order> Order { get; set; }
}
And in my Customer Controller I have defined
ICustomerRepository customerRepository;
IOrderRepository orderRepository;
public CustomersController()
: this(new CustomerRepository())
{
}
public CustomersController(ICustomerRepository repository)
{
customerRepository = repository;
}
public CustomersController(IOrderRepository repository)
{
orderRepository = repository;
}
then I try to pass the CustomerAndOrdersViewModel to the details view
public ActionResult Details(int id)
{
QuestionAndOrderViewModel viewdata = new CustomerAndOrdersViewModel(customerRepository.GetCustomer(id),
orderRepository.FindAllOrders());
return View(viewdata);
}
But I am receiving the error "Object reference not set to an instance of an object" for orderRepository.
Where I am going wrong?