(使用另一个视图进行编辑)[非部分视图]。单击“保存”后...应打开同一页面,其中包含已更新的记录,而不是第一页。
控制器中的索引方法
public ActionResult Index(int? page)
{
StudentDBHandle dbhandle = new StudentDBHandle();
ModelState.Clear();
return View(dbhandle.GetStudent().ToList().ToPagedList(page ?? 1,5));
}
在控制器中编辑方法
//获取:学生/编辑/5
public ActionResult Edit(int id)
{
StudentDBHandle sdb = new StudentDBHandle();
return View(sdb.GetStudent().Find(smodel => smodel.UserId == id));
}
**// POST: Student/Edit/5**
[HttpPost]
public ActionResult Edit(int id, StudentModel smodel)
{
try
{
StudentDBHandle sdb = new StudentDBHandle();
sdb.UpdateDetails(smodel);
return RedirectToAction("Index");
}
catch
{
return View();
}
}
我已在索引视图中启用分页:
@Html.PagedListPager(Model, page => Url.Action("Index", new { page }),
new PagedListRenderOptions() { Display = PagedListDisplayMode.IfNeeded ,DisplayPageCountAndCurrentLocation = true})
我想将页码从编辑视图传递到索引控制器,以便显示我选择要更新的记录的同一页面。
当我在索引视图中使用 @{ViewBag.CurrentPage = page } 时,它显示页面不会在当前上下文中出现的错误。
帮助纠正我错误的地方
调用
RedirectToAction
时,您可以将参数作为匿名对象传递。您必须将当前页面发布到编辑操作,以便可以将其传递。
[HttpPost]
public ActionResult Edit(int id, StudentModel smodel, int? currentPage){
try {
StudentDBHandle sdb = new StudentDBHandle();
sdb.UpdateDetails(smodel);
return RedirectToAction("Index", "MyController", new { page = currentPage } );
}
catch {
return View();
}
}