【发布时间】:2015-01-07 19:17:50
【问题描述】:
我有一个 CustomersController,在 Detail.cshtml 视图中我需要显示该客户的联系人列表和该客户的位置列表。我有一个 Detail(int? id) ActionResult 并且我可以从控制器内访问我的 CustomerService。截至目前,在我的 Detail ActionResult 中,我能够做到:
var cust = _custService.GetCustomerById(id);
return View(cust);
如何建议将我的其余列表显示在视图中。我想我会在我的 CustomerService 中创建一个 GetContacts(customerID)、GetLocations(CustomerID),然后像上面调用 GetCustomerByID 一样调用它们。如果我这样做,我将如何访问这些列表。
接下来我考虑的可能是创建一个 ViewModel,它包含所有基本客户属性,例如 customer.Name、customer.Phone,但随后我试图弄清楚如何确保 ViewModel 具有客户的联系人和位置.我是否只需向 ViewModel 添加 2 个属性,例如 customer.contacts 和 customer.locations,因为 EF6 将使它们可供我使用?
关于在视图中获取与实体相关的多个信息列表的最佳方法有什么建议吗?客户联系人和客户位置都是一对多的
【问题讨论】:
标签: asp.net asp.net-mvc controller asp.net-mvc-5 repository