Asp.net mvc 2 ASP.net MVC2从局部视图访问两个表
我在寻找从一个局部视图访问两个数据库表的最佳方法的建议 我有一个视图模型,包括两个对象列表,客户和客户站点Asp.net mvc 2 ASP.net MVC2从局部视图访问两个表,asp.net-mvc-2,list,viewmodel,Asp.net Mvc 2,List,Viewmodel,我在寻找从一个局部视图访问两个数据库表的最佳方法的建议 我有一个视图模型,包括两个对象列表,客户和客户站点 public class CustomerSitesListViewModel { public IList<CustomerSite> CustomerSites { get; set; } public PagingInfo PagingInfo { get; set; } public IList<Customer> customers
public class CustomerSitesListViewModel
{
public IList<CustomerSite> CustomerSites { get; set; }
public PagingInfo PagingInfo { get; set; }
public IList<Customer> customers { get; set; }
}
是否仍然可以访问客户属性,即customers.CustomerName
这是MVC的一个方面,我真的不明白,我已经坚持了一个多星期了,如果有人能提供任何建议,我将不胜感激
问候
利亚姆
更新=======
我添加了以下视图模型
客户视图模型
public class CustomerViewModel
{
public int Id { get; set; }
public string CustomerName { get; set; }
public string PrimaryContactName { get; set; }
public string PrimaryContactNo { get; set; }
public SiteViewModel Site { get; set; }
}
public class SiteViewModel
{
public int Id { get; set; }
public int CustomerId { get; set; }
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
}
站点视图模型
public class CustomerViewModel
{
public int Id { get; set; }
public string CustomerName { get; set; }
public string PrimaryContactName { get; set; }
public string PrimaryContactNo { get; set; }
public SiteViewModel Site { get; set; }
}
public class SiteViewModel
{
public int Id { get; set; }
public int CustomerId { get; set; }
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
}
主视图模型
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<CustomerDatabase.WebUI.Models.CustomerSitesListViewModel>" %>
<ul id="list-of-sites">
<li><%: Model.AddressLine1 %></li>
<li><%: Model.AddressLine2%></li>
<li><%: Model.AddressLine3%></li>
public class CustomerSitesListViewModel
{
public IList<CustomerSite> CustomerSites { get; set; }
public PagingInfo PagingInfo { get; set; }
public IList<Customer> customers { get; set; }
public IEnumerable<CustomerViewModel> Customers { get; set; }
}
你能就我的错误提出建议吗
非常感谢
Liam问题在于您没有使用视图模型。您刚刚定义了一些
CustomerSitesListViewModel
类,在这些类中,您将域模型填充为属性,这不适合此特定视图的要求
因此,首先要做的是定义一个干净的视图模型。您说过您需要访问客户和客户站点的部分属性。所以他们之间一定有某种联系
让我们从定义此视图模型开始:
public class SiteViewModel
{
public string AddressLine1 { get; set; }
}
public class CustomerViewModel
{
public string Name { get; set; }
public SiteViewModel Site { get; set; }
}
public class CustomerSitesListViewModel
{
public IEnumerable<CustomerViewModel> Customers { get; set; }
}
最后是主视图模型:
public class SiteViewModel
{
public string AddressLine1 { get; set; }
}
public class CustomerViewModel
{
public string Name { get; set; }
public SiteViewModel Site { get; set; }
}
public class CustomerSitesListViewModel
{
public IEnumerable<CustomerViewModel> Customers { get; set; }
}
然后是将强类型化到CustomersiteListViewModel
的主视图:
<%= Html.DisplayFor(x => x.Customers) %>
@Darin Dimitrov,您好,谢谢您的回复,我按照您的指示操作,但仍然无法正常工作,我更新了上面的帖子。如果您能给我更多建议,我将不胜感激。@liam,仔细阅读我的帖子。
CustomerSitesListViewModel.ascx
强类型为CustomerSitesListViewModel
。在那里你可以访问它的属性。x.Customers)%%>
位在Index.aspx
视图中。@Darin Dimitrov,感谢我在视图模型中大力键入了它,我现在已经可以工作了,但我必须将公共SiteViewModel站点{get;set;}添加到CustomersiteListViewModel中,并删除Ienumerable语法才能访问这两个站点objects@Darin迪米特罗夫,现在我面临着如何在视图中的for each循环中呈现模板。@Darin Dimitrov,当使用模板时,我是否只需要视图中的这一行x.Customers)%>?因为属性是在模板中定义的。
public class CustomerSitesListViewModel
{
public IEnumerable<CustomerViewModel> Customers { get; set; }
}
public ActionResult Index()
{
// TODO: query repositories and map the domain objects
// they returned to the actual view model. Can't help much
// here as I don't know how are your domain models and repositories
// organized
var viewModel = new CustomerSitesListViewModel
{
Customers = ...
};
return View(viewModel);
}
<%= Html.DisplayFor(x => x.Customers) %>
<%@ Control
Language="C#"
Inherits="System.Web.Mvc.ViewUserControl<CustomerSitesListViewModel>"
%>
<%= Html.DisplayFor(x => x.Name) %>
<%= Html.DisplayFor(x => x.Site.AddressLine1) %>