The*_*heo 5 asp.net-mvc paging pagedlist
我正在使用PagedList.Mvc,并且添加了一种在mvc Web应用程序中的各个页面之间导航的好方法。但是,当我单击“编辑”或“详细信息”选项卡并保存更改时,我将被发送回第一页。我想保留在进行更改的同一页面上。
这是我在控制器中的代码:
// GET: Item
public ActionResult Index(int? page)
{
var items = db.Items.Include(i => i.PurchaseOrder);
return View(items.ToList().ToPagedList(page ?? 1, 3));
}
Run Code Online (Sandbox Code Playgroud)
这是视图中的代码:
@using PagedList;
@using PagedList.Mvc;
@model IPagedList<PurchaseOrders.Models.Item>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.First().ItemDescription)
</th>
<th>
@Html.DisplayNameFor(model => model.First().Quantity)
</th>
<th>
@Html.DisplayNameFor(model => model.First().Price)
</th>
<th>
@Html.DisplayNameFor(model => model.First().DueDate)
</th>
<th>
@Html.DisplayNameFor(model => model.First().DateReceived)
</th>
<th>
@Html.DisplayNameFor(model => model.First().Comments)
</th>
<th>
@Html.DisplayNameFor(model => model.First().PurchaseOrder.PurchaseRequest_)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.ItemDescription)
</td>
<td>
@Html.DisplayFor(modelItem => item.Quantity)
</td>
<td>
@Html.DisplayFor(modelItem => item.Price)
</td>
<td>
@Html.DisplayFor(modelItem => item.DueDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.DateReceived)
</td>
<td>
@Html.DisplayFor(modelItem => item.Comments)
</td>
<td>
@Html.DisplayFor(modelItem => item.PurchaseOrder.PurchaseRequest_)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.ItemId }) |
@Html.ActionLink("Details", "Details", new { id=item.ItemId }) |
@Html.ActionLink("Delete", "Delete", new { id=item.ItemId })
</td>
</tr>
}
</table>
@Html.PagedListPager(Model, page => Url.Action("Index", new { page }))
Run Code Online (Sandbox Code Playgroud)
请帮忙!
小智 6
您可以将一个额外的“ page”参数传递给您的编辑方法,例如
在您的Index方法中,添加
ViewBag.CurrentPage = page; // or use a view model property
Run Code Online (Sandbox Code Playgroud)
然后您的链接将是
@Html.ActionLink("Edit", "Edit", new { id=item.ItemId, page = ViewBag.CurrentPage})
Run Code Online (Sandbox Code Playgroud)
然后你的编辑方法
[HttpGet]
public ActionResult Edit(int ID, int page)
{
ViewBag.CurrentPage = page; // pass current page to edit view
Run Code Online (Sandbox Code Playgroud)
还有你的编辑视图
@using (Html.BeginForm(new { page = ViewBag.CurrentPage })) {
Run Code Online (Sandbox Code Playgroud)
并在您发布方法
[HttpGet]
public ActionResult Edit(EditModel model, int page)
{
.... // Save
return RedirectToAction("Index", new { page = page });
Run Code Online (Sandbox Code Playgroud)