基本上我已经在本教程的帮助下完成了所有的排序,过滤和分页,这非常非常方便,因为我对这个材料很新。 - 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
控制器:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using SunsUniversity.Models;
using SunsUniversity.DAL;
using PagedList;
using PagedList.Mvc;
namespace SunsUniversity.Controllers
{
public class StudentController : Controller
{
private SchoolContext db = new SchoolContext();
// GET: /Student/
public ViewResult Index()
{
var students = from s in db.Students
select s;
return View(students.ToList());
}
// GET: /Student/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Student student = db.Students.Find(id);
if (student == null)
{
return HttpNotFound();
}
return View(student);
}
// GET: /Student/Create
public ActionResult Create()
{
return View();
}
// POST: /Student/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include="ID,LastName,FirstMidName,EnrollmentDate")] Student student)
{
try
{
if (ModelState.IsValid)
{
db.Students.Add(student);
db.SaveChanges();
return RedirectToAction("Index");
}
}
catch (DataException /* dex */)
{
//Log the error (uncomment dex variable name and add a line here to write a log.
ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator.");
}
return View(student);
}
// GET: /Student/Edit/5
public ActionResult Edit(int? id)
{
var model = TempData["Index"];
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Student student = db.Students.Find(id);
if (student == null)
{
return HttpNotFound();
}
return View(student);
}
// POST: /Student/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include="ID,LastName,FirstMidName,EnrollmentDate")] Student student)
{
if (ModelState.IsValid)
{
db.Entry(student).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(student);
}
// GET: /Student/Delete/5
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Student student = db.Students.Find(id);
if (student == null)
{
return HttpNotFound();
}
return View(student);
}
// POST: /Student/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
Student student = db.Students.Find(id);
db.Students.Remove(student);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
}
指数:
@model IEnumerable<SunsUniversity.Models.Student>
@{
ViewBag.Title = "Students";
}
<h2>@ViewBag.Title</h2>
<p class="indexOptions">
@Html.ActionLink("Back", "Index", "Home") @Html.ActionLink("Create New", "Create")
</p>
<table class="table table-striped table-hover">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.LastName)
</th>
<th>
@Html.DisplayNameFor(model => model.FirstMidName)
</th>
<th>
@Html.DisplayNameFor(model => model.EnrollmentDate)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.LastName)
</td>
<td>
@Html.DisplayFor(modelItem => item.FirstMidName)
</td>
<td>
@Html.DisplayFor(modelItem => item.EnrollmentDate)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
@Html.ActionLink("Details", "Details", new { id=item.ID }) |
@Html.ActionLink("Delete", "Delete", new { id=item.ID })
</td>
</tr>
}
</tbody>
<tfoot>
<tr>
<th class="filterable"></th>
<th></th>
<th class="filterable"></th>
<th class="filterable"></th>
<th></th>
<th></th>
</tr>
</tfoot>
</table>
@section Outro{
<script>
$(document).ready(function () {
var table = $('.table').DataTable();
$(".table tfoot th").each(function (i) {
if ($(this).hasClass("filterable")) {
var select = $('<select class="form-control"><option value="">Filter by</option></select>')
.appendTo($(this).empty())
.on('change', function () {
var val = $(this).val();
table.column(i)
.search(val ? '^' + $(this).val() + '$' : val, true, false)
.draw();
});
table.column(i).data().unique().sort().each(function (d, j) {
if (d.length > 0) {
select.append('<option value="' + d + '">' + d + '</option>');
}
});
}
});
});
</script>
}
现在我的问题是: 我的应用程序用户询问包含表的页面是否可以记住表的过滤器,排序顺序和当前页面(因为当他们单击表项来执行任务然后返回到它时他们会喜欢它是“因为他们离开了它”)
Cookie似乎是前进的方向,但是如何让页面加载这些页面并在它发出第一个数据请求之前在表格中设置它们在这个阶段有点超出我的范围。
有没有人有这种经历的经验?谢谢!
最终可以在索引文件中添加很少的东西
保存偏好设置:从$(window).unload(function(){ ... });
加载偏好设置:从$(document).ready(function(){ ... });
答案 0 :(得分:0)
最简单的方法是在会话存储,cookie或视图包中保存要进入的过滤器,排序和页面。您可以将所有呼叫的参数发送回服务器返回页面。 EF不支持您想要的操作,因为它只关心获取和设置数据。您必须在您的代码中管理分页,排序和过滤。
您引用的示例通过使用PagedList(nuget包)
的这些参数来处理此问题public ViewResult Index(string sortOrder, string currentFilter, string searchString, int? page)
这些参数就是他们所说的,并且会在您导航到的每个页面上发回。
完整方法代码:
public ViewResult Index(string sortOrder, string currentFilter, string searchString, int? page)
{
ViewBag.CurrentSort = sortOrder;
ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date";
if (searchString != null)
{
page = 1;
}
else
{
searchString = currentFilter;
}
ViewBag.CurrentFilter = searchString;
var students = from s in db.Students
select s;
if (!String.IsNullOrEmpty(searchString))
{
students = students.Where(s => s.LastName.Contains(searchString)
|| s.FirstMidName.Contains(searchString));
}
switch (sortOrder)
{
case "name_desc":
students = students.OrderByDescending(s => s.LastName);
break;
case "Date":
students = students.OrderBy(s => s.EnrollmentDate);
break;
case "date_desc":
students = students.OrderByDescending(s => s.EnrollmentDate);
break;
default: // Name ascending
students = students.OrderBy(s => s.LastName);
break;
}
int pageSize = 3;
int pageNumber = (page ?? 1);
return View(students.ToPagedList(pageNumber, pageSize));
}