我基本上是在试着回到我来的页面.例如,在保存更改后从https://po/POes/Edit/1321返回到https://po/?pg=10,而不是被重定向回第1页?

为了更清楚地说明这个问题:如何从编辑页面传递ID和页码并编辑HTTPPOST控制器,以便将用户重定向回索引页10?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Policy;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.Build.Framework;
using Microsoft.EntityFrameworkCore;
using POV1.Data;
using POV1.Models;

namespace POV1.Controllers
{
    public class POesController : Controller
    {
        private readonly MVCDemoDbContext _context;

        public POesController(MVCDemoDbContext context)
        {
            _context = context;
        }        

        public IActionResult Index(int pg = 1)
        {
            List<PO> po = _context.POT.OrderByDescending(b => b.Id).ToList();
            const int pageSize = 10;
            if (pg < 1)
                pg = 1;

            int recsCount = po.Count();
            int hour = DateTime.Now.Hour;
            ViewBag.Greeting = hour < 12 ? "Good Morning" : "Good Afternoon";
            var pager = new Pager(recsCount, pg, pageSize);

            int recSkip = (pg - 1) * pageSize;
            var data = po.Skip(recSkip).Take(pager.PageSize).ToList();

            this.ViewBag.Pager = pager;
            
            return View(data);
        }
   
        // POST: POes/Edit/5
        // To protect from overposting attacks, enable the specific properties you want to bind to.
        // For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        
        public async Task<IActionResult> Edit(int id,    [Bind("Id,Supplier,Description,DateTimeItemReleased,Employee,Additional")] PO pO)
        {
            if (id != pO.Id)
            {
                return NotFound();
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(pO);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!POExists(pO.Id))
                    {
                        return NotFound();
                    }
                    else
                    {
                        throw;
                    }
                }
                //return RedirectToAction(nameof(Index));
                return RedirectToAction("Details", new { id = pO.Id });
            }
            return View(pO);
        }
    }
}

我try 了以下几种方法:

return RedirectToAction("Index", new { id = pO.Id });

推荐答案

看起来你在这里混淆了页码和id的概念. 虽然你可能能够做到这一点,但你需要跟踪页码(比如在ViewBag或具有适当属性的适当ViewModel中),或者执行一些复杂的逻辑来找到项目所在的页面,然后将其发送到具有适当重载签名的Index视图.

或者只使用分页对象:

public class Pagination
{
    public int PageNumber { get; set; } = 1;
    public int PageSize{ get; set; } = 10;
    public int TotalItems{ get; set; }
    public bool SortDirection{ get; set; } = SortDirection.Ascending;
}

public enum SortDirection { Ascending, Descending }

然后重定向

return RedirectToAction("Index", new Pagination() { PageSize = pageSize, PageNumber = pageNumber });

类似这样的东西:

public IActionResult Index(PaginationObject paginationObject)
{
     // rest here to returned the list of page items
}

Csharp相关问答推荐

将XPS转换为PDF C#

ITypeLib2.GetLibStatistics()在C#中总是抛出AccessViolationException

在路由中使用枚举

获取具有AutoFaces的所有IOptions对象的集合

Blazor在FluentButton onClick事件上设置参数

我的MRG32k3a算法实现返回的数字超出了预期的[0,1]范围

N层解决方案上的依赖注入-删除样板

依赖项注入、工厂方法和处置困境

在两个已具有一对多关系的表之间添加另一个一对多关系

C#多键字典和TryGetValue

当我将`ConcurentDictionary`转换为`IDictionary`时,出现了奇怪的并发行为

此异步方法在重写方法中缺少等待运算符警告

如何使用.NET Aspire从Blazor应用程序与GRPC API通信?

用于请求用户返回列表的C#Google API

C#无法将.csv列转换为用于JSON转换的列表

如何正确地在VB中初始化类?

从HTML元素获取 colored颜色

Xamarin Forms应用程序中登录页面的用户名和密码编辑文本之间不需要的空格

最小API定义的Swagger标头参数

使用c#中的Windows 10关机消息