我可以显示所有客户的列表,我可以显示所有订单的列表,我想更进一步。
我想在同一页面上呈现两个部分视图,以显示与该客户相关的客户详细信息和订单。
<%@ 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); %>
我创建了一个视图模型
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; }
}
在我的客户控制器中,我定义了
ICustomerRepository customerRepository;
IOrderRepository orderRepository;
public CustomersController()
: this(new CustomerRepository())
{
}
public CustomersController(ICustomerRepository repository)
{
customerRepository = repository;
}
public CustomersController(IOrderRepository repository)
{
orderRepository = repository;
}
然后我尝试将 CustomerAndOrdersViewModel 传递给详细信息视图
public ActionResult Details(int id)
{
QuestionAndOrderViewModel viewdata = new CustomerAndOrdersViewModel(customerRepository.GetCustomer(id),
orderRepository.FindAllOrders());
return View(viewdata);
}
但是我收到 orderRepository 的错误“对象引用未设置为对象的实例”。
我哪里错了?