RedirectToAction没有调用动作

时间:2019-05-29 02:41:55

标签: c# asp.net-core

为什么索引操作未在RedirectToAction上调用Foo?我设置了一个断点,但似乎并没有达到这个目的。应该注意的是我的模型状态是有效的,所以这不是问题。

    [HttpPost]
    public ActionResult Index(Home model)
    {
        if (!ModelState.IsValid)
        {
            return View(model);
        }
        TempData["model"] = model;
        return RedirectToAction("Foo");
    }

    public ActionResult Foo()
    {
        Home model = (Home)TempData["model"];
        return View();
    }

2 个答案:

答案 0 :(得分:0)

也许您可以尝试

[HttpGet]
public ActionResult Foo()
{
    Home model = (Home)TempData["model"];
    return View();
}

然后您可以使用3种方式重定向

RedirectToAction("ActionName", "ControllerName"); // if calling from other controller

return RedirectToAction("ActionName");

return Redirect("/ControllerName/ActionName"); 

让我知道您是否仍然有任何问题

答案 1 :(得分:0)

如果您使用的是Same控制器,或者如果您不想重定向 只是返回视图。因为我在演示应用程序中检查了您的代码,并尝试重现您的问题。但是您的代码可以正常工作

[HttpPost]
    public ActionResult Index(Home model)
    {
        if (!ModelState.IsValid)
        {
            return View(model);
        }
        TempData["model"] = model;
        return View("Foo");
    }

如果它不起作用,那么您可以检查您的路线,并确保没有定义任何特殊的路线来使事情中断。  或在您的Global.asax内添加此方法并进行调试。

protected void Application_Error(object sender, EventArgs e)
{
    Exception ex = Server.GetLastError();
}