How to stay / persist on one page when using PagedList.mvc

I am using PagedList.Mvc and I have added a nice way to navigate different pages in the mvc web application. However, when I click on the “edit” or “details” tab and save the changes, I return to the first page. I want to stay on the same page where the changes were made.

Here is the code I have in the controller:

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

Here is the code that I have in the 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 }))

Please, help!

+4
source share
2 answers

You can pass an additional page parameter to your editing method, for example

In the Index method add

ViewBag.CurrentPage = page; // or use a view model property

Then your link will be

@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 })) {

post

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

ViewBag, (ViewBag ).

, 1 . null , 1- .

, (Edit/Create), . TempData, HTTP , viewbag viewData. , , . . BTW, TempData . : :

var page = TempData["page"];

Create Edit Submit

    //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;

TempData [ "page" ] , :

var page = TempData["page"];  
return View(items.ToList().ToPagedList(page ?? 1, 3)); 
+1

All Articles