【问题标题】:MVC3 Redirect to route from ActionResultMVC3 重定向到来自 ActionResult 的路由
【发布时间】:2011-11-07 11:53:56
【问题描述】:

所以我有一个 HttpPost 只有 ActionResult 称为 Edit。在完成它的事情(逻辑等)之后,我希望它重定向到另一个控制器。让我们说HomeController。这里是:

[HttpPost]
public ActionResult Edit(Chair chair, string xml)
{
    if (ModelState.IsValid)
    {
        try
        {
            _repository.EditChair(chair, xml);
            return RedirectToRoute(new { contoller = "Home", action = "index"});
        }
        catch (Exception ex)
        {
            //error msg for failed edit in XML file
            ModelState.AddModelError("", "Error editing record. " + ex.Message);
        }
    }
    return View(Chair);

}

我尝试过其他方法,例如 return RedirectResult()RedirectToAction()RedirectToRoute("string") - 但它仍然不断从控制器返回索引视图,Edit 方法位于 (ChairController) 中。

这样做的正确方法是什么??

【问题讨论】:

  • 这里已经有类似的东西了... [RedirectToRoute 应该如何使用?][1] 希望它有所帮助... [1]:stackoverflow.com/questions/1290355/…
  • @Jan 谢谢。它似乎对帖子中的那个人有用,但不适合我:S FML
  • @Kasper 猜我误解了你的问题。我以为你说返回相同的动作
  • @David 和所有其他人。没关系。原来是VS10搞砸了。我见过的最奇怪的事情。查看我的答案以获取更多信息。

标签: c# asp.net-mvc-3 actionresult url-redirection


【解决方案1】:

错别字:

contoller = "Home"

应该是

controller = "Home"

或:

return RedirectToAction("index", "home");

【讨论】:

  • 糟糕。错字失败。但是,正如我的问题所述,已经尝试过 RedirectToAction()。没用。
  • @Kasper Skov,除非你的路线定义搞砸了,return RedirectToAction("index", "home"); 工作得很好。
【解决方案2】:

哇,这是有史以来最奇怪的事情。代码是正确的(正如我确定的那样)。我尝试再次调试它,并注意到当我浏览代码时,调试器thingo 只标记了一些代码:return RedirectToAction("Index", 它实际上停在那里,并没有通过"Home");。我还注意到我的断点实际上是黄色的,并且告诉我源代码与原始代码不一样?什么什么?它一直在说,通过数百次保存、重新启动、构建和重建。它出乎意料地接受了代码,我的断点变成了红色,代码工作得很好。

真的很抱歉耽误了你们的时间!

【讨论】:

  • 你可能忘了重启你的开发服务器:-P
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-18
  • 1970-01-01
  • 2021-09-09
相关资源
最近更新 更多