在我的MVC3剃须刀页面上,我使用webgrid显示大约100多条记录的列表。
我将页面大小设置为10,并且分页和排序工作正常。我唯一的问题是在我的页面上我有一个"删除" webgrid上的超链接。当我点击"删除"时,它将删除记录并重新加载页面。但是当重新加载页面时,它会返回到page1
例如,如果我在第4页并点击"删除",它会删除该记录并进入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");
}
这是我的webgrid
@{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 }))
));
}