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

标签 asp.net-mvc paging pagedlist

我正在使用 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/

相关文章:

memory-management - 在内存分配中 - 12 位页面偏移量实际上是做什么用的?

asp.net-mvc - 类型存在于两个库中

c# - Asp.net core Sakura.AspNetCore.PagedList 和部分 View

c# - 类型 'IEnumerable<>' 在未引用的程序集中定义

html - 如何在 ASP.NET MVC 中获取移动设备的 CSS 宽度?

jquery - 使用 ASP.NET MVC 和 jquery 实现 AJAX 的最佳方法是什么?

c# - ASP.NET MVC 4 : Only allow one request at a time

asp.net - GridView 分页问题

sql-server - 分布式二级缓存与专注于调优数据库相比有何优缺点

linux - x86 CR3 在 SPARC 架构中对应的寄存器是什么?