为什么它在return View(contacts.ToPagedList(pageNumber, pageSize));
语句中给出错误 Index 方法中的错误:
The method 'Skip' is only supported for sorted input in LINQ to Entities. The method 'OrderBy' must be called before the method 'Skip'.
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PhoneBook.Models;
using PagedList;
namespace PhoneBook.Controllers
{
public class ContactsController : Controller
{
private PhoneDBContext db = new PhoneDBContext();
//
// GET: /Contacts/
public ViewResult Index(string searchString, string sortOrder, Contact model, string currentFilter, int? page)
{
ViewBag.CurrentSort = sortOrder;
ViewBag.FNameSortParm = sortOrder == "FName asc"? "FName desc" : "FName asc";
ViewBag.DateSortParm = sortOrder == "Date asc" ? "Date desc" : "Date asc";
ViewBag.LNameSortParm = sortOrder == "LName asc" ? "LName desc" : "LName asc";
ViewBag.CompSortParm = sortOrder == "Company asc" ? "Company desc" : "Company asc";
ViewBag.MobSortParm = sortOrder == "Mob asc" ? "Mob desc" : "Mob asc";
ViewBag.TelSortParm = sortOrder == "Tel asc" ? "Tel desc" : "Tel asc";
if (Request.HttpMethod == "GET") { searchString = currentFilter; }
else {page = 1;}
ViewBag.CurrentFilter = searchString;
var contacts = from m in db.Contacts
select m;
switch (sortOrder)
{
case "FName desc":
contacts = contacts.OrderByDescending(s => s.FirstName);
break;
case "FName asc":
contacts = contacts.OrderBy(s => s.FirstName);
break;
case "LName desc":
contacts = contacts.OrderByDescending(s => s.LastName);
break;
case "LName asc":
contacts = contacts.OrderBy(s => s.LastName);
break;
case "Company desc":
contacts = contacts.OrderByDescending(s => s.Company);
break;
case "Company asc":
contacts = contacts.OrderBy(s => s.Company);
break;
case "Date desc":
contacts = contacts.OrderByDescending(s => s.DateAdded);
break;
case "Date asc":
contacts = contacts.OrderBy(s => s.DateAdded);
break;
case "Mob desc":
contacts = contacts.OrderByDescending(s => s.MobileNumber);
break;
case "Mob asc":
contacts = contacts.OrderBy(s => s.MobileNumber);
break;
case "Tel desc":
contacts = contacts.OrderByDescending(s => s.TelephoneNumber);
break;
case "Tel asc":
contacts = contacts.OrderBy(s => s.TelephoneNumber);
break;
}
if (!String.IsNullOrEmpty(searchString))
{
contacts = contacts.Where(s => s.LastName.ToUpper().Contains(searchString)||s.FirstName.ToUpper().Contains(searchString)||s.Company.ToUpper().Contains(searchString));
}
int pageSize = 3;
int pageNumber = (page ?? 1);
return View(contacts.ToPagedList(pageNumber, pageSize));
}
//
// GET: /Contacts/Details/5
public ViewResult Details(int id)
{
Contact contact = db.Contacts.Find(id);
return View(contact);
}
//
// GET: /Contacts/Create
public ActionResult Create()
{
return View();
}
//
// POST: /Contacts/Create
[HttpPost]
public ActionResult Create(Contact contact)
{
if (ModelState.IsValid)
{
db.Contacts.Add(contact);
contact.DateAdded = DateTime.Now;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(contact);
}
//
// GET: /Contacts/Edit/5
public ActionResult Edit(int id=0)
{
Contact contact = db.Contacts.Find(id);
if (contact == null) { return HttpNotFound(); } // returns blank page if id is not valid
return View(contact);
}
//
// POST: /Contacts/Edit/5
[HttpPost]
public ActionResult Edit(Contact contact)
{
if (ModelState.IsValid)
{
db.Entry(contact).State = EntityState.Modified;
contact.DateAdded = DateTime.Now;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(contact);
}
//
// GET: /Contacts/Delete/5
public ActionResult Delete(int id)
{
Contact contact = db.Contacts.Find(id);
if (contact == null) { return HttpNotFound(); }
return View(contact);
}
//
// POST: /Contacts/Delete/5
[HttpPost, ActionName("Delete")]
public ActionResult DeleteConfirmed(int id)
{
Contact contact = db.Contacts.Find(id);
if (contact == null) { return HttpNotFound(); }
db.Contacts.Remove(contact);
db.SaveChanges();
return RedirectToAction("Index");
}
public ActionResult SearchIndex(string searchString)
{
var contacts = from m in db.Contacts
select m;
if (!String.IsNullOrEmpty(searchString))
{
contacts = contacts.Where(s => s.LastName.Contains(searchString));
}
return View(contacts);
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
}
}
下面是 Index.cshtml 代码:
@model PagedList.IPagedList<PhoneBook.Models.Contact>
@{
ViewBag.Title = "Phone Book";
}
<p>
@using (Html.BeginForm()){
<p> Search: @Html.TextBox("SearchString", ViewBag.CurrentFilter as string)
<input type="submit" value="Go" /></p>
}
</p>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
@Html.ActionLink("First Name", "Index", new { sortOrder=ViewBag.FNameSortParm, currentFilter=ViewBag.CurrentFilter })
</th>
<th>
@Html.ActionLink("Last Name", "Index", new { sortOrder = ViewBag.LNameSortParm, currentFilter = ViewBag.CurrentFilter })
</th>
<th>
@Html.ActionLink("Mobile Num", "Index", new { sortOrder = ViewBag.MobSortParm, currentFilter = ViewBag.CurrentFilter })
</th>
<th>
@Html.ActionLink("Tel Num", "Index", new { sortOrder = ViewBag.TelSortParm, currentFilter = ViewBag.CurrentFilter })
</th>
<th>
@Html.ActionLink("Company", "Index", new { sortOrder = ViewBag.CompSortParm, currentFilter = ViewBag.CurrentFilter })
</th>
<th>
@Html.ActionLink("Date Added/Updated", "Index", new { sortOrder = ViewBag.DateSortParm, currentFilter = ViewBag.CurrentFilter })
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.FirstName)
</td>
<td>
@Html.DisplayFor(modelItem => item.LastName)
</td>
<td>
@Html.DisplayFor(modelItem => item.MobileNumber)
</td>
<td>
@Html.DisplayFor(modelItem => item.TelephoneNumber)
</td>
<td>
@Html.DisplayFor(modelItem => item.Company)
</td>
<td>
@Html.DisplayFor(modelItem => item.DateAdded)
</td>
<td>
@Html.ActionLink("Details", "Details", new { id=item.ID })
@Html.ActionLink("Edit", "Edit", new { id=item.ID })
@Html.ActionLink("Delete", "Delete", new { id=item.ID })
</td>
</tr>
}
</table>
<div>
Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber)
of @Model.PageCount
@if (Model.HasPreviousPage)
{
@Html.ActionLink("<<", "Index", new { page = 1, sortOrder = ViewBag.CurrentSort, currentFilter=ViewBag.CurrentFilter })
@Html.Raw(" ");
@Html.ActionLink("< Prev", "Index", new { page = Model.PageNumber - 1, sortOrder = ViewBag.CurrentSort, currentFilter=ViewBag.CurrentFilter })
}
else
{
@:<<
@Html.Raw(" ");
@:< Prev
}
@if (Model.HasNextPage)
{
@Html.ActionLink("Next >", "Index", new { page = Model.PageNumber + 1, sortOrder = ViewBag.CurrentSort, currentFilter=ViewBag.CurrentFilter })
@Html.Raw(" ");
@Html.ActionLink(">>", "Index", new { page = Model.PageCount, sortOrder = ViewBag.CurrentSort, currentFilter=ViewBag.CurrentFilter })
}
else
{
@:Next >
@Html.Raw(" ")
@:>>
}
</div>