我基本上是在试着回到我来的页面.例如,在保存更改后从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 });