【发布时间】:2016-02-04 09:15:45
【问题描述】:
我目前正在学习 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 然后我想出了错误说:
CS1061:“PagedList.IPagedList”不包含“Title”的定义,并且找不到接受“PagedList.IPagedList”类型的第一个参数的扩展方法“Title”(您是否缺少 using 指令或程序集参考?)
我在 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());
}
这是我的 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 }))
哇,我刚刚在错误列表中又发现了 5 个错误...
错误 4“PagedList.IPagedList”不包含“Location”的定义,并且找不到接受“PagedList.IPagedList”类型的第一个参数的扩展方法“Location”(您是否缺少 using 指令或程序集参考?) c:\Users\Harry\Documents\Visual Studio 2013\Projects\MvcMovie\MvcMovie\Views\Movies\Index.cshtml 30 41 MvcMovie
错误 5 无法从用法中推断方法“System.Web.Mvc.Html.DisplayNameExtensions.DisplayNameFor(System.Web.Mvc.HtmlHelper, System.Linq.Expressions.Expression>)”的类型参数。尝试明确指定类型参数。 c:\Users\Harry\Documents\Visual Studio 2013\Projects\MvcMovie\MvcMovie\Views\Movies\Index.cshtml 33 6 MvcMovie
我觉得很痛苦,因为我在这里卡了5个多小时哈哈
【问题讨论】:
-
将
@Html.DisplayNameFor(model => model.Title)更改为@Html.DisplayNameFor(m => m[0].Title)(其他表格标题同上) -
哇!有效!!!!非常感谢!!!
标签: c# asp.net-mvc entity-framework ef-code-first asp.net-mvc-scaffolding