在我的 MVC3 razor 页面上,我使用 webgrid 来显示大约 100 多条记录的列表。
我将页面大小设置为 10,并且分页和排序工作正常。我唯一的问题是在我的页面上我在 webgrid 上有一个“删除”超链接。当我单击“删除”时,它将删除记录并重新加载页面。但是当重新加载页面时它会返回到 page1
例如,如果我在 page4 上并单击“删除”,它会删除该记录并进入 page1,但我想保留在 page4 本身中。关于如何实现这一目标的任何建议。
public ViewResult Index(int? page, string sort)
{
var startPage = 0;
if (page.HasValue && page.Value > 0)
{
startPage = page.Value - 1;
}
vm.Employees = GetAllEmployeeData(startPage, vm.PageSize, sort);
vm.TotalRecords = context.TableEmployee.Count();
return View(vm);
}
public IEnumerable<Employees> GetAllEmployeeData(int page, int records, string sort)
{
IEnumerable<Employees> EmployeeData = null;
EmployeeData = context.TableEmployee.Where(e => e.IsActivated == true)
.Select(e => new Employees{ EmpID = e.EmpID, Name= e.Name, Joiningdate = e.Joiningdate , Department = e.DeptName}).AsEnumerable<Employees>();
switch (sort)
{
case "Department":
EmployeeData = EmployeeData .OrderBy(r => r.Department);
break;
case "JoiningDate":
EmployeeData = EmployeeData .OrderBy(r => r.JoiningDate);
break;
default:
EmployeeData = EmployeeData .OrderBy(r => r.Name);
break;
}
EmployeeData = EmployeeData .Skip(page * records).Take(records).ToList();
return EmployeeData ;
}
public ActionResult DeleteSelectedEmployee(int empId)
{
ContentsViewModel model = new ContentsViewModel();
int timelineItemId = 0;
EmployeeData E = context.TableEmployee.Find(empId);
context.TableEmployee.Remove(E);
context.SaveChanges();
return RedirectToAction("Index");
}
这是我的网络网格
@{var webgrid = new WebGrid(canPage: true, rowsPerPage: Model.PageSize, canSort: true, ajaxUpdateContainerId: "grid");
webgrid.Bind(Model.Employees, rowCount: Model.TotalRecords, autoSortAndPage: false);
webgrid.Pager(WebGridPagerModes.Numeric);
@webgrid.GetHtml(tableStyle: "webgridTable", htmlAttributes: new { id = "grid" },
columns: webgrid.Columns(
webgrid.Column("Name", "Name"),
webgrid.Column("Department", "Department"),
webgrid.Column("JoiningDate", "Join Date", format: @<text>@item.JoiningDate.ToString("dddd, MMMM d, yyyy")</text>),
webgrid.Column(header: "Action", format: (item) => Html.ActionLink("Edit", "Edit", new { empId = item.EmpID })),
webgrid.Column(format: (item) => Html.ActionLink("Delete", "Delete Record", new { empId = item.EmpID }))
));
}