gpt4 book ai didi

asp.net-mvc - 使用 PagedList.mvc 时如何保持/保留在同一页面上

转载 作者:行者123 更新时间:2023-12-02 03:54:14 28 4
gpt4 key购买 nike

我正在使用 PagedList.Mvc,并且添加了一种在 mvc Web 应用程序中跨各个页面进行导航的好方法。但是,当我单击“编辑”或“详细信息”选项卡并保存更改时,我会返回到第一页。我想保留在进行更改的同一页面上。

这是我在 Controller 中的代码:

// GET: Item
public ActionResult Index(int? page)
{
var items = db.Items.Include(i => i.PurchaseOrder);
return View(items.ToList().ToPagedList(page ?? 1, 3));
}

这是我在 View 中的代码:

    @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

以及您的编辑 View

 @using (Html.BeginForm(new { page = ViewBag.CurrentPage })) {

并在您发布的方法中

[HttpGet]
public ActionResult Edit(EditModel model, int page)
{
.... // Save
return RedirectToAction("Index", new { page = page });

关于asp.net-mvc - 使用 PagedList.mvc 时如何保持/保留在同一页面上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25271610/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com