我基本上是试图回到我来自的页面。例如,从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);
}
}
}
字符串
我尝试了以下方法:
return RedirectToAction("Index", new { id = pO.Id });
型
1条答案
按热度按时间sq1bmfud1#
看起来你在这里混合了页码和id的概念。虽然你可能能够做到这一点,但你需要跟踪页码(比如在ViewBag或具有适当属性的适当ViewModel中),或者执行一些复杂的逻辑来找到项目福尔斯所在的页面,然后将其发送到具有适当重载签名的Index视图。
或者只使用分页对象:
字符串
然后重定向
型
比如说
型