2014-11-06 105 views
-1

我試着在我的項目中使用視圖模型,但我有此錯誤:模型產品類型PagedList.PagedList [],但本詞典需要類型的模型項目

The model item passed into the dictionary is of type 'PagedList.PagedList 1[ContosoUniversity.Models.Course]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable 1[ContosoUniversity.ViewModels.CourseViewModel]'.

控制器:

public ActionResult Index(int? page) 
    { 
     int pageNumber = page ?? 1; 

     var courses = courseService.GetAll(); 

     var coursesViewList = Mapper.Map<IEnumerable<Course>, IEnumerable<CourseViewModel>>(courses); 

     var model = coursesViewList.ToPagedList(pageNumber, PageSize); 

     return View(model); 
    } 

查看:

@model IEnumerable<ContosoUniversity.ViewModels.CourseViewModel> 
@using PagedList.Mvc; 
@using PagedList; 


@{ 
    ViewBag.Title = "Index"; 
} 

<h2>Index</h2> 

<p> 
    @Html.ActionLink("Create New", "Create") 
</p> 

<table class="table"> 
    <tr> 
     <th> 
      @Html.DisplayNameFor(model => model.Title) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.Credits) 
     </th> 
     <th> 
      Students 
     </th> 
     <th></th> 
    </tr> 

@foreach (var item in Model) { 
    <tr> 
     <td> 
      @Html.DisplayFor(modelItem => item.Title) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.Credits) 
     </td> 
     <td> 
      @string.Join(",", item.Enrollments.Select(e => e.Student.FullName)) 
     </td> 
     <td> 
      @Html.ActionLink("Edit", "Edit", new { id=item.CourseID }) | 
      @Html.ActionLink("Delete", "Delete", new { id=item.CourseID }) 
     </td> 
    </tr> 
} 

</table> 


@Html.PagedListPager((IPagedList)Model, page => Url.Action("Index", new { page })) 

可能有人幫我解決一個問題好嗎?

+1

錯誤是不言自明的,請嘗試 明白它。在你的控制器中放置斷點並檢查各種變量的內容。 – CodeCaster 2014-11-06 10:38:59

回答

1

您正在通過型號PagedList.PagedList1<ContosoUniversity.Models.Course>的型號,而您的查看是強制輸入IEnumerable<ContosoUniversity.ViewModels.CourseViewModel>

您需要通過從行動IEnumerable<T>或設置視圖模型PagedList<T>或者您可以通過以下方式包裹它一個ViewModel內解決這個問題:

public class CoursesVM 
{ 
    public PagedList.IPagedList<CourseViewModel> Courses { get; set; } 
} 

在行動:

public ActionResult Index(int? page) 
{ 
    int pageNumber = page ?? 1; 

    var courses = courseService.GetAll(); 

    var coursesViewList = Mapper.Map<IEnumerable<Course>, IEnumerable<CourseViewModel>>(courses); 

    var model = new CoursesVM(); 
    model.Courses = coursesViewList.ToPagedList(pageNumber, PageSize); 

    return View(model); 
} 

並在視圖:

@model ContosoUniversity.ViewModels.CoursesVM 
@using PagedList.Mvc; 
@using PagedList; 


@{ 
    ViewBag.Title = "Index"; 
} 

<h2>Index</h2> 

<p> 
    @Html.ActionLink("Create New", "Create") 
</p> 

<table class="table"> 
    <tr> 
     <th> 
      @Html.DisplayNameFor(model => model.Title) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.Credits) 
     </th> 
     <th> 
      Students 
     </th> 
     <th></th> 
    </tr> 

@foreach (var item in Model.Courses) { 
    <tr> 
     <td> 
      @Html.DisplayFor(modelItem => item.Title) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.Credits) 
     </td> 
     <td> 
      @string.Join(",", item.Enrollments.Select(e => e.Student.FullName)) 
     </td> 
     <td> 
      @Html.ActionLink("Edit", "Edit", new { id=item.CourseID }) | 
      @Html.ActionLink("Delete", "Delete", new { id=item.CourseID }) 
     </td> 
    </tr> 
} 

</table> 
+0

謝謝,但現在不同的錯誤:CS1579:foreach語句不能對'ContosoUniversity.ViewModels.CourseViewModel'類型的變量進行操作,因爲'ContosoUniversity.ViewModels.CourseViewModel'不包含'getEnumerator'的公共定義 – Warlock 2014-11-06 11:03:18

+0

您正在迭代錯誤迭代其屬性列表不在單個對象上 – 2014-11-06 11:20:01

相關問題