0

I'm currently studying ASP.NET tutorial and I was trying to implement this tutorial to my project. 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 I wrote them in my VS, pressed F5 and then I came up with error saying:

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?)

My index method in MoviesController.cs:

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

and this is my Index.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 }))

Wow, and I just came up with 5 more errors on error list...

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

I feel miserable because I got stuck here for more than 5 hours haha

Harry Hur
  • 1
  • 1

1 Answers1

1

Well, your error message is quite self-explanatory. You have declared a model of type PagedList.IPagedList<MvcMovie.Models.Movie>, but then have the following in your Razor page:

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

While the items contained in the enumeration do, IPagedList<T> itself does not contain a property Location, thus the error.

Perhaps you meant to either enclose the above in a loop, take the First() item in the enumeration, or create an encapsulating model containing both the IPagedList and other properties?

lc.
  • 113,939
  • 20
  • 158
  • 187