gpt4 book ai didi

asp.net-mvc - 如何让 MVC POST 返回上一页?

转载 作者:行者123 更新时间:2023-12-03 23:28:02 25 4
gpt4 key购买 nike

我有以下操作,它是从带有记录列表的屏幕调用的。

    [HttpPost]
//[Authorize(Roles = "admin")]
public ActionResult Edit(EditViewModel itemView)
{

操作完成后,我想返回调用操作的页面。但是我不想刷新该页面。我只想使用类似于浏览器中的“上一个”按钮的东西返回到填充的屏幕。现在,当我单击“保存”时,我的操作会执行以下操作,这不是我想要的:
return RedirectToAction("Index");

有什么方法可以使用MVC3重定向到上一页吗?

在我单击编辑以编辑返回到帖子的答案后,类似于 Stackoverflow 功能。

最佳答案

而不是重定向,

[HttpGet]
public ActionResult Index()
{
/// preform any processing necessary for your index page on GET
return View("Index");
}

[HttpPost]
public ActionResuit Edit(EditViewModel itemView)
{
if (ModelState.IsValid)
{
/// do whatever you want with your model...
}

// return the contents as they'd be rendered by the Index action
return Index();
}

请注意,使用此方法,浏览器中的 URL 仍将显示编辑 url(如 /area_name/edit),但您可以通过以下方式修复该问题:
  • 使用重定向(您说过不想这样做)
  • 使用 JavaScript 更新 URL,或使用 history.back()正如@AlanStephens 建议的那样
  • 可能没有立即想到的其他方法。

  • 但是,我怀疑这是否真的是最好的方法。通常,用户期望不同的 URL 做不同的事情。

    或者,如果我理解正确,并且正在从索引页面调用编辑操作,
    [HttpGet]
    public ActionResult Index()
    {
    return View();
    }

    [HttpPost] /// from a form on Index
    public ActionResult Index(EditViewModel model)
    {
    if (ModelState.IsValid)
    {
    ////
    }
    return View();
    }

    就拿 /Edit完全没戏了。同样,我并不真正关心这种方法。

    关于asp.net-mvc - 如何让 MVC POST 返回上一页?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7984379/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com