如何在ASP.NET MVC 4中正确使用元组

我正在尝试使用元组在一个视图中使用两个模型,但我收到此错误:’/’应用程序中的服务器错误。

传递到字典中的模型项的类型为’PagedList.PagedList 1[S00117372CA3.Product]', but this dictionary requires a model item of type 'System.Tuple 2 [PagedList.IPagedList 1[S00117372CA3.Product],System.Collections.Generic.IEnumerable 1[S00117372CA3.Product]', but this dictionary requires a model item of type 'System.Tuple 1[S00117372CA3.Product],System.Collections.Generic.IEnumerable 1 [S00117372CA3.Order]]’。

描述:执行当前Web请求期间发生未处理的exception。 请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

exception详细信息:System.InvalidOperationException:传递到字典中的模型项的类型为“PagedList.PagedList 1[S00117372CA3.Product]', but this dictionary requires a model item of type 'System.Tuple 2 1[S00117372CA3.Product]', but this dictionary requires a model item of type 'System.Tuple [PagedList.IPagedList 1[S00117372CA3.Product],System.Collections.Generic.IEnumerable 1 [S00117372CA3.Order]]’。

这是我的代码:

  @*@model PagedList.IPagedList*@ @model Tuple<PagedList.IPagedList, IEnumerable> @{ ViewBag.Title = "Index"; } 

Index

@Html.ActionLink("Create New", "Create")

@foreach (var item in Model.Item1) { }
@Html.DisplayNameFor(model => model.Item1[0].ProductName) @Html.DisplayNameFor(model => model.Item1[0].Supplier.CompanyName) @Html.DisplayNameFor(model => model.Item1[0].Category.CategoryName) @Html.DisplayNameFor(model => model.Item1[0].QuantityPerUnit) @Html.DisplayNameFor(model => model.Item1[0].UnitPrice) @Html.DisplayNameFor(model => model.Item1[0].UnitsInStock) @Html.DisplayNameFor(model => model.Item1[0].UnitsOnOrder) @Html.DisplayNameFor(model => model.Item1[0].ReorderLevel) @Html.DisplayNameFor(model => model.Item1[0].Discontinued)
@Html.DisplayFor(modelItem => item.ProductName) @Html.DisplayFor(modelItem => item.Supplier.CompanyName) @Html.DisplayFor(modelItem => item.Category.CategoryName) @Html.DisplayFor(modelItem => item.QuantityPerUnit) @Html.DisplayFor(modelItem => item.UnitPrice) @Html.DisplayFor(modelItem => item.UnitsInStock) @Html.DisplayFor(modelItem => item.UnitsOnOrder) @Html.DisplayFor(modelItem => item.ReorderLevel) @Html.DisplayFor(modelItem => item.Discontinued) @Html.ActionLink("Edit", "Edit", new { id=item.ProductID }) | @Html.ActionLink("Details", "Details", new { id=item.ProductID }) | @Html.ActionLink("Delete", "Delete", new { id=item.ProductID })
Page @(Model.Item1.PageCount < Model.Item1.PageNumber ? 0 : Model.Item1.PageNumber) of @Model.Item1.PageCount @if (Model.Item1.HasPreviousPage) { @Html.ActionLink("<<", "Index", new { page = 1}) @Html.Raw(" "); @Html.ActionLink("< Prev", "Index", new {page = Model.Item1.PageNumber - 1}) } else{ @:<< @Html.Raw(" "); @:", "Index", new {page = Model.Item1.PageNumber + 1}) @Html.Raw(" "); @Html.ActionLink(">>", "Index", new {page = Model.Item1.PageCount}) } else{ @:Next > @Html.Raw(" ") @:>> }
@foreach (var item in Model.Item2) { @Html.Partial("_Orders") }

这是我的控制器代码:

 var q = (from o in db.Order_Details where o.ProductID == id join ord in db.Orders on o.OrderID equals ord.OrderID orderby o.Quantity descending select ord); var qm = q.Take(3); return PartialView("_Orders", qm.ToList()); 

从本质上讲,您只是尝试将多个模型传递给您的视图。 由于您只能将一种类型传递给视图,因此执行所需操作的方法是将模型包装在视图模型中,然后将其传递给视图。 就像是:

 public class ProductViewModel { public IPagedList Products { get; set; } public IEnumerable Orders { get; set; } } public ActionResult Index() { var model = new ProductViewModel(); model.Products = // ... fetch from database model.Orders = // ... fetch from database return View(model); } 

现在可以对视图模型强类型化视图:

 @model ProductViewModel