【发布时间】:2014-01-14 02:48:31
【问题描述】:
我正在尝试使用元组在一个视图中使用两个模型,但出现此错误: “/”应用程序中的服务器错误。
传入字典的模型项的类型为“PagedList.PagedList
1[S00117372CA3.Product]', but this dictionary requires a model item of type 'System.Tuple2[PagedList.IPagedList1[S00117372CA3.Product],System.Collections.Generic.IEnumerable1[S00117372CA3.Order]]”。
说明:在执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。
异常详情:System.InvalidOperationException:传入字典的模型项的类型为“PagedList.PagedList
1[S00117372CA3.Product]', but this dictionary requires a model item of type 'System.Tuple2[PagedList.IPagedList1[S00117372CA3.Product],System.Collections.Generic.IEnumerable1[S00117372CA3.Order]]”。
这是我的代码:
@*@model PagedList.IPagedList<S00117372CA3.Product>*@
@model Tuple<PagedList.IPagedList<S00117372CA3.Product>, IEnumerable<S00117372CA3.Order>>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.Item1[0].ProductName)
</th>
<th>
@Html.DisplayNameFor(model => model.Item1[0].Supplier.CompanyName)
</th>
<th>
@Html.DisplayNameFor(model => model.Item1[0].Category.CategoryName)
</th>
<th>
@Html.DisplayNameFor(model => model.Item1[0].QuantityPerUnit)
</th>
<th>
@Html.DisplayNameFor(model => model.Item1[0].UnitPrice)
</th>
<th>
@Html.DisplayNameFor(model => model.Item1[0].UnitsInStock)
</th>
<th>
@Html.DisplayNameFor(model => model.Item1[0].UnitsOnOrder)
</th>
<th>
@Html.DisplayNameFor(model => model.Item1[0].ReorderLevel)
</th>
<th>
@Html.DisplayNameFor(model => model.Item1[0].Discontinued)
</th>
<th></th>
</tr>
@foreach (var item in Model.Item1) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.ProductName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Supplier.CompanyName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Category.CategoryName)
</td>
<td>
@Html.DisplayFor(modelItem => item.QuantityPerUnit)
</td>
<td>
@Html.DisplayFor(modelItem => item.UnitPrice)
</td>
<td>
@Html.DisplayFor(modelItem => item.UnitsInStock)
</td>
<td>
@Html.DisplayFor(modelItem => item.UnitsOnOrder)
</td>
<td>
@Html.DisplayFor(modelItem => item.ReorderLevel)
</td>
<td>
@Html.DisplayFor(modelItem => item.Discontinued)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.ProductID }) |
@Html.ActionLink("Details", "Details", new { id=item.ProductID }) |
@Html.ActionLink("Delete", "Delete", new { id=item.ProductID })
</td>
</tr>
}
</table>
<div>
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(" ");
@:< Prev
}
@if (Model.Item1.HasNextPage)
{
@Html.ActionLink("Next >", "Index", new {page = Model.Item1.PageNumber + 1})
@Html.Raw(" ");
@Html.ActionLink(">>", "Index", new {page = Model.Item1.PageCount})
}
else{
@:Next >
@Html.Raw(" ")
@:>>
}
</div>
@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());
【问题讨论】:
-
为什么要使用
Tuple?将两个对象包装在一个封闭类中。它更好......并且也可以与模型绑定器一起使用。 -
因为我不知道该怎么做。如果您可以发布一个示例和一个链接,那就太棒了。谢谢
-
问题是,在您的控制器中,您将错误的数据类型传递给视图。你的控制器代码是什么?
-
嗨 Keith,发布了相关操作结果的控制器代码
-
另外,我应该说部分视图只有在单击详细信息链接后才会被调用。我这不是正确的吗?
标签: c# asp.net asp.net-mvc-4 tuples models