0

我目前正在學習ASP.NET教程,並試圖將本教程實現到我的項目中。 http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/sorting-filtering-and-paging-with-the-entity-framework-in-an-asp-net-mvc-application 我寫了他們在我的VS,按下F5 後來我想出了錯誤說:在MoviesController.csMVC編譯錯誤:添加分頁...?

CS1061: 'PagedList.IPagedList' does not contain a definition for 'Title' and no extension method 'Title' accepting a first argument of type 'PagedList.IPagedList' could be found (are you missing a using directive or an assembly reference?)

我的指數方法:

public ActionResult Index(string sortOrder,string currentFilter, string searchString, int? page) { 
     ViewBag.CurrentSort = sortOrder; 
     ViewBag.IDSortParm = String.IsNullOrEmpty(sortOrder) ? "ID_desc" : ""; 
     ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date"; 
     ViewBag.CurrentFilter = searchString; 
     var movies = from m in db.Movies 
           select m; 

     if (searchString != null) { 
      page = 1; 
     } 
     else { 
      searchString = currentFilter; 
     } 

     if (!String.IsNullOrEmpty(searchString)) { 
      movies = movies.Where(s => s.Title.Contains(searchString)); 
     } 
     switch (sortOrder) { 
      case "ID_desc": 
       movies = movies.OrderByDescending(m => m.ID); 
       break; 
      case "Date": 
       movies = movies.OrderBy(m => m.Date); 
       break; 
      case "date_desc": 
       movies = movies.OrderByDescending(m => m.Date); 
       break; 
      default: //Name ascending 
       movies = movies.OrderBy(m => m.ID); 
       break; 
     } 
     int pageSize = 10; 
     int pageNumber = (page ?? 1); 
     return View(movies.ToPagedList(pageNumber, pageSize)); 
     //return View(movies.ToList()); 
    } 

,這是我的指數。 CSHTML

@model PagedList.IPagedList<MvcMovie.Models.Movie> 
@using PagedList.Mvc; 
<link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" /> 
@{ 
ViewBag.Title = "For Sale"; 
} 
<h2>For Sale</h2> 
<p> 
@Html.ActionLink("Post", "Create") 
@using (Html.BeginForm("Index", "Movies", FormMethod.Get)) { 
<p> 
    Search by Title: @Html.TextBox("SearchString", ViewBag.CurrentFilter as string) 
    <input type="submit" value="Search" /> 
</p> 
} 
</p> 

<table class="table"> 
    <tr> 
     <th> 
      @Html.ActionLink("ID", "Index", new { sortOrder = ViewBag.IDSortParm, currentFilter = ViewBag.CurrentFilter }) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.Title) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.Price) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.Location) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.Name) 
     </th> 
     <th> 
      @Html.ActionLink("Date", "Index", new { sortOrder = ViewBag.DateSortParm, currentFilter = ViewBag.CurrentFilter }) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.ViewCount) 
     </th> 
     <th></th> 
    </tr> 

    @foreach (var item in Model) { 
     <tr> 
      <td> 
       @Html.DisplayFor(modelItem => item.ID) 
      </td> 
      <td> 
       @Html.ActionLink(item.Title, "Details", new { id = item.ID }) 
       @*@Html.DisplayFor(modelItem => item.Title)*@ 
      </td> 
      <td> 
       @Html.DisplayFor(modelItem => item.Price) 
      </td> 
      <td> 
       @Html.DisplayFor(modelItem => item.Location) 
      </td> 
      <td> 
       @Html.DisplayFor(modelItem => item.Name) 
      </td> 
      <td> 
       @Html.DisplayFor(modelItem => item.Date) 
      </td> 
      <td> 
       @Html.DisplayFor(modelItem => item.ViewCount) 
      </td> 
      <td> 
       @Html.ActionLink("Edit", "Edit", new { id = item.ID }) | 
       @Html.ActionLink("Delete", "Delete", new { id = item.ID }) 
      </td> 
     </tr> 
    } 

</table> 
<br /> 
Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount 

@Html.PagedListPager(Model, page => Url.Action("Index", 
    new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter })) 

哇,我只是想出了5個錯誤的錯誤列表...

Error 4 'PagedList.IPagedList' does not contain a definition for 'Location' and no extension method 'Location' accepting a first argument of type 'PagedList.IPagedList' could be found (are you missing a using directive or an assembly reference?) c:\Users\Harry\Documents\Visual Studio 2013\Projects\MvcMovie\MvcMovie\Views\Movies\Index.cshtml 30 41 MvcMovie

Error 5 The type arguments for method 'System.Web.Mvc.Html.DisplayNameExtensions.DisplayNameFor(System.Web.Mvc.HtmlHelper, System.Linq.Expressions.Expression>)' cannot be inferred from the usage. Try specifying the type arguments explicitly. c:\Users\Harry\Documents\Visual Studio 2013\Projects\MvcMovie\MvcMovie\Views\Movies\Index.cshtml 33 6 MvcMovie

我感到痛苦,因爲我被困在這裏5個多小時哈哈

+0

將'@ Html.DisplayNameFor(model => model.Title)'改爲'@ Html.DisplayNameFor(m => m [0] .Title)'(其他表標題同上) –

+0

哇!有效!!!!非常感謝!!! –

回答

1

那麼,你的錯誤信息是相當不言自明的。您已聲明PagedList.IPagedList<MvcMovie.Models.Movie>類型的模型,但隨後在你的剃刀頁面如下:

@Html.DisplayNameFor(model => model.Location) 

雖然包含在枚舉的項目做,IPagedList<T>本身不包含財產Location,因此錯誤。

也許你打算把上面的代碼放在一個循環中,在枚舉中取First()項,或者創建一個包含IPagedList和其他屬性的封裝模型?