我正在使用 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));
}
这是我在视图中的代码:
@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 }))
例如,您可以将额外的“page”参数传递给您的编辑方法
在您的 Index 方法中,添加
ViewBag.CurrentPage = page; // or use a view model property
那么您的链接将是
@Html.ActionLink("Edit", "Edit", new { id=item.ItemId, page = ViewBag.CurrentPage})
然后是你的编辑方法
[HttpGet]
public ActionResult Edit(int ID, int page)
{
ViewBag.CurrentPage = page; // pass current page to edit view
以及您的编辑视图
@using (Html.BeginForm(new { page = ViewBag.CurrentPage })) {
并在您发布的方法中
[HttpGet]
public ActionResult Edit(EditModel model, int page)
{
.... // Save
return RedirectToAction("Index", new { page = page });
在这种情况下,页面存储在 ViewBag 中,这使其成为短暂的(ViewBag 仅适用于当前请求)。
在控制器中,如果您得到 null,则告诉它使用 1 作为当前页面。所以 null 总是会被重新调整,并且每次都会得到第一页。
您需要向导航到(编辑/创建)的视图提供当前页码,然后在完成后将其返回到原始页面。 您可以使用 TempData,它在 HTTP 重定向上运行良好,并且比 viewbag 或 viewData 的寿命更长。 您还可以在调用操作时将其与模型一起移动,然后将其返回给需要页码的索引操作。 您也可以使用会话。顺便说一句,TempData 在幕后使用会话。 更新: 要添加到索引操作中的代码:
var page = TempData["page"];
要在“创建”或“编辑提交”操作中添加的代码
//Get the page number
var page = TempData["page"];
//Set it back to Tempdata (because Tempdata is only for redirects) otherwise it will be lost
TempData["page"]=page;
再次回调index动作时,将参数的值添加到TempData["page"]中 您还可以直接从索引操作访问它,因为我们重新填充了它:
var page = TempData["page"];
return View(items.ToList().ToPagedList(page ?? 1, 3));
我也遇到这个问题了。
我一开始尝试将其放入 URL 中,但在我们的 URL 中包含
?page=2
似乎有点奇怪。
所以我用
TempData
替换了它
你需要做的是:
当您处于
TempData
操作方法中时,将页面存储在 Index()
中;
public const string PAGE_QUERY_STRING_KEY = "page";
public ActionResult Index(int page = 1)
{
TempData[PAGE_QUERY_STRING_KEY] = page;
...
}
然后在其他任何地方使用
TempData.Peek()
,而不是 TempData[]
(在与当前 Index
页面相关的请求之间保留页面的值)--- 在编辑、创建、详细信息等中行动方法:
public ActionResult Edit(...)
{
...
return RedirectToAction(nameof(Index), new { page = TempData.Peek(PAGE_QUERY_STRING_KEY) });
// do not do this because this will remove the temp data
// return RedirectToAction(nameof(Index), new { page = TempData[PAGE_QUERY_STRING_KEY])
}
...您认为:
<!--(Edit.cshtml)-->
...
<p>
@Html.ActionLink("Back to List", "Index",
new { page = TempData.Peek(FLP.Web.Controllers.UsersAdminController.PAGE_QUERY_STRING_KEY) })
</p>