我想知道是否有一種方法可以將多個列表傳遞給一個視圖來呈現。asp.mvc傳遞多個列表來查看
這是我在把PeopleController
public ActionResult Index()
{
EmployeeContext db = new EmployeeContext();
//Sites
List<SelectListItem> listSelectListItem = new List<SelectListItem>();
foreach (Sites loc in db.Locations)
{
SelectListItem selectListItem = new SelectListItem()
{
Text = loc.HRSite,
Value = loc.HRSite
};
listSelectListItem.Add(selectListItem);
}
SiteViewModel siteViewModel = new SiteViewModel();
siteViewModel.Sites = listSelectListItem;
//Cost Centers
List<SelectListItem> listSelectListItem2 = new List<SelectListItem>();
foreach (CostCenter cc in db.CostCenterNumbers)
{
SelectListItem selectListItem = new SelectListItem()
{
Text = cc.CostCenterNumber,
Value = cc.CostCenterNumber
};
listSelectListItem2.Add(selectListItem);
}
CCViewModel ccViewModel = new CCViewModel();
ccViewModel.CostCenter = listSelectListItem2;
List<object> myModel = new List<object>();
myModel.Add(siteViewModel);
myModel.Add(ccViewModel);
return View(myModel);
下面的代碼是我的看法:
@model IEnumerable<object>
@{
List<MVCDemo.Models.CostCenter> lstCostCenter = Model.ToList()[0] as List<MVCDemo.Models.CostCenter>;
List<MVCDemo.Models.Sites> lstLocation = Model.ToList()[1] as List<MVCDemo.Models.Sites>;
}
<h3>Cost Center</h3>
<ul>
@foreach (var item in lstCostCenter)
{
<li>@item.CostCenterNumber</li>
}
</ul>
<hr />
<h3>Site</h3>
<ul>
@foreach (var item in lstLocation)
{
<li>@item.HRSite</li>
}
</ul>
我怎樣才能改變含有2個列表框,而不是2 「名單」 的觀點?
改進的代碼可讀性,我猜測代碼可以改進以更好地理解 –