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

标签 c# asp.net asp.net-mvc-4 tuples models

我正在尝试使用一个元组在一个 View 中使用两个模型,但出现此错误: “/”应用程序中的服务器错误。

The model item passed into the dictionary is of type 'PagedList.PagedList1[S00117372CA3.Product]', but this dictionary requires a model item of type 'System.Tuple2[PagedList.IPagedList1[S00117372CA3.Product],System.Collections.Generic.IEnumerable1[S00117372CA3.Order]]'.

描述:当前网络请求执行过程中出现未处理的异常。请查看堆栈跟踪以获取有关错误及其在代码中的来源的更多信息。

Exception Details: System.InvalidOperationException: The model item passed into the dictionary is of type 'PagedList.PagedList1[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")
}

这是我请求的 Controller 代码:

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());

最佳答案

本质上,您只是想将多个模型传递给您的 View 。由于您只能将一种类型传递给 View ,因此执行所需操作的方法是将模型包装在 View 模型中,然后将其传递给 View 。像这样的东西:

public class ProductViewModel
{
    public IPagedList<Product> Products { get; set; }
    public IEnumerable<Order> Orders { get; set; }
}

public ActionResult Index()
{
    var model = new ProductViewModel();
    model.Products = // ... fetch from database
    model.Orders = // ... fetch from database

    return View(model);
}

View 现在可以强类型化到 View 模型:

@model ProductViewModel

关于c# - 如何在 ASP.NET MVC 4 中正确使用元组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21105216/

相关文章:

c# - C#获取线程ID的方法

asp.net - 无法获取数据库中保存的文本文件的内容(My-Sql)

c# - 在处理请求之前如何用asp读取json post的内容?

css - MVC4 : On validation error, 将文本框颜色更改为红色

c# - 为什么我的 HttpPostedFileBase 总是空的?

c# - 如何使用 WndProc 捕获 ESC 按键?

c# - 在 WinRT 中从 C# 解析 JSON

c# - 在给定 HWND 的情况下如何获取窗口的子窗口?

c# - 在 C#.NET 中切换语言对站点没有影响

c# - 修改 DAL 时出错,System.ArgumentException, "An entry with the same key already exist"