2014-01-14 2 views
3

튜플을 사용하여 한 뷰에서 두 모델을 사용하려고하지만이 오류가 발생합니다. '/'응용 프로그램의 서버 오류입니다.ASP.NET MVC 4에서 튜플을 올바르게 사용하는 방법

The model item passed into the dictionary is of type '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.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") 
} 

여기의 요청에 따라 내 컨트롤러 코드 :

Exception Details: System.InvalidOperationException: The model item passed into the dictionary is of type '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.Order]]'.

여기 내 코드는 기본적으로

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

가 왜'Tuple'를 사용

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

뷰는 현재 뷰 모델에 강력한 입력 할 수 있습니다? 둘러싸는 클래스의 양쪽 모두의 오브젝트를 랩합니다. 그것은 훨씬 더 좋네 .. 그리고 모델 바인더와도 작동합니다. –

+0

나는 그 일을하는 법을 모르기 때문에. 환상적인 예와 링크를 게시 할 수 있다면. 감사합니다 –

+0

문제는 컨트롤러에서 잘못된 데이터 형식을보기로 전달하고 있다는 것입니다. 귀하의 컨트롤러 코드는 무엇입니까? –

답변

6

, 당신은 단지보기에 여러 모델을 통과하기 위해 노력하고 있습니다. 하나의 유형 만보기에 전달할 수 있기 때문에 원하는 것을 수행하는 방법은 모델을보기 모델에 랩핑하여보기에 전달하는 것입니다. 같은 뭔가 :

@model ProductViewModel 
+0

IPagedList는 .net과 함께 제공되는 새로운 클래스입니다. – Thomas

+0

@Thomas [github] (https://github.com/troygoode/PagedList/blob/master/src/PagedList/IPagedList.cs)에서 찾을 수 있습니다. –

관련 문제