【问题标题】:RedirectToAction called twice issueRedirectToAction 调用了两次问题
【发布时间】:2011-12-10 14:02:27
【问题描述】:

我有控制器:

Controller A
{
   public ActionResult ExecuteSomeStuff()
   {
    ....use TempData
    returns a View("StuffMade", SomeModel);
   }
}

 Controller B
{
   public ActionResult DoStuff()
   {
         ...fill up TempData     


    returns RedirectToAction("ExecuteSomeStuff", "A");
   }
}

问题是控制器 A 上的 ExecuteSomeStuff 方法执行了两次。 我不需要进行实际的重定向,我只希望为 DoStuff 方法返回来自 ExecuteSomeStuff 方法的结果(视图)。 我不想在控制器 B 中引用控制器 A 来直接调用该方法。

有没有办法做到这一点,而无需在控制器 B 中对控制器 A 进行物理重定向或新引用??

【问题讨论】:

    标签: asp.net-mvc model-view-controller redirect


    【解决方案1】:

    如果我的理解正确,您可以从控制器 B 返回 ExecuteSomeStuff View 而不是 RedirectToAction。您需要能够设置 Model 以从控制器 B 传递到视图.所以你可能还需要参考Controller A中使用的方法。

    Controller B:
     {
    
        public ActionResult DoStuff()
        {
        ...fill up TempData 
        //set the model then Return the View with the Model passed in
        return View("ExecuteSomeStuff","A",SomeModel);
        }
    

    更新

    这将查找与操作 ExecuteSomeStuff 同名的 View,而无需实际输入 ExecuteSomeStuff 操作。如果您的视图有不同的名称,您可以明确指定要返回的视图,如下所示:

    return View("../A/theCorrectView.aspx", model);
    

    【讨论】:

    • 我这样做了,它抛出异常:“这里不允许使用相对虚拟路径'ExecuteSomeStuff.aspx'”,所以我认为它没有到达 ExecuteSomeStuff 操作方法。
    • 你是对的,它不执行 ExecuteSomeStuff 动作。我以为这是最初的想法。我更新了我的答案。您使用的是哪个版本的 MVC?我已经对此进行了测试,它适用于 MVC3。它也应该在 MVC 2 中工作。
    猜你喜欢
    • 2017-07-30
    • 1970-01-01
    • 2021-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-03
    • 1970-01-01
    • 2017-06-25
    相关资源
    最近更新 更多