2010-02-08 51 views
24

我已经使用RedirectToAction在我完成了一个帖子到控制器并保存后,但网址没有改变,重定向似乎不起作用。我需要补充的是,当我调试时,重定向会输入控制器操作方法。它不会更改网址或导航到Index视图...RedirectToAction不能正常工作

public ViewResult Index() 
    { 
     return View("Index",new TrainingViewModel()); 
    } 

public ActionResult Edit() 
{ 
    //save the details and return to list 
    return RedirectToAction("Index"); 

} 

我在做什么错?

public static void RegisterRoutes(RouteCollection routes) 
     { 
      routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 
      routes.IgnoreRoute("{resource}.js/{*pathInfo}"); 

      routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" }); 

      routes.MapRoute(
       "Default", // Route name 
       "{controller}/{action}/{id}", // URL with parameters 
       new { controller = "Home", action = "Index", id = "" } // Parameter defaults 
       ); 
     } 

// JQUERY CALLS

this.SynchValuesToReadOnly = function() { 
     window.location = $('#siteRoot').attr('href') + 'Sites/'; 

    }; 

    this.OnSiteEdit = function() { 

     //post back to the server and update the assessment details  
     var options = { 
      target: '', 
      type: 'post', 
      url: '/Site/Edit', 
      beforeSubmit: othis.validate, 
      success: othis.SynchValuesToReadOnly 
     }; 

     $('#uxSiteForm').ajaxSubmit(options); 
       }; 
+7

您显示的代码是正确的。我疯狂的猜测是你没有做一个标准的POST(例如,重定向不适用于AJAX文章)。你能展示更多的代码吗? – 2010-02-08 21:37:41

+0

aaaaahhh ....不知道。你是对的。它确实来自jQuery中的ajax文章,这意味着如果成功,我应该从那里重定向。没问题。把你的评论作为一个“答案”,我会标记当我证明它的作品,如果你喜欢 谢谢 – kurasa 2010-02-08 21:41:49

回答

65

您显示的代码是正确的。我疯狂的猜测是你没有做一个标准的POST(例如,重定向不适用于AJAX文章)。

浏览器将忽略对AJAX POST的重定向响应。如果您在AJAX调用返回重定向响应时需要重定向,则由脚本重定向。

+0

好的答案。你能解释我们为什么吗? – 2012-07-06 18:04:35

+2

当你做一个异步'POST'时,如果你得到一个重定向响应,你必须手动重定向(在JS中)。浏览器不会自动执行此操作。这就是所有这些。在这种情况下,浏览器假定脚本负责。 – 2012-07-06 18:16:44

+2

只是补充和给像我这样的n00bs一些指导。在ajax.beginform指令中,包含像这样的新AjaxOptions {OnSuccess =“window.location.href ='ControllerName/Action'”}和voilá – 2014-02-04 23:45:53

3

检查与萤火虫。 Post可能会返回服务器错误。

7

您需要添加“return false;”结束你的onclick javascript事件。例如:

剃刀代码

@using (Html.BeginForm()) 
{ 
    @Html.HiddenFor(model => model.ID) 
    @Html.ActionLink("Save", "SaveAction", "MainController", null, new { @class = "saveButton", onclick = "return false;" }) 
} 

jQuery代码

$(document).ready(function() { 
     $('.saveButton').click(function() { 
      $(this).closest('form')[0].submit(); 
     }); 
    }); 

C#

[AcceptVerbs(HttpVerbs.Post)] 
public ActionResult SaveAction(SaveViewModel model) 
{ 
    return RedirectToAction("Index"); 
} 
+4

出于某种原因,我总是忘记“返回”是必需的。猜猜我很习惯旧的Response.Redirect,从来不需要“返回”。 – 2012-11-01 19:49:40

1

尝试如下:

回报在你的行动:

return Json(Url.Action("Index", "Home"));

,并在你的Ajax:

window.location.href

4

我只是碰到这个问题,没有什么是为我工作。原来,我将控制器重定向到所需的授权 - [Authorize]属性阻止了任何重定向。

[Authorize]//comment this out 
public class HomeController : Controller {...} 

在路上或在较大的项目,这可能不是所期望的修复,但如果你只是寻找一个简单的重定向或开始了,这可能是你的解决方案。

2

这是有点晚....但我有同样的问题在MVC5。我有两个同名的动作,这很常见。我没有收到任何可以在控制器的Application_Error或OnException中捕获的错误。我尝试在RedirectToAction路由变量的action,controller,area中添加无效。我最终将POST操作重命名为EditPost,现在一切正常。希望这可以帮助。

public ActionResult Edit(int id) 
{ 
    return View(); 
} 

[HttpPost, ValidateAntiForgeryToken] 
public ActionResult Edit(MyInputModel myInputModel) 
{ 
    return View(); 
} 

public ActionResult Create() 
{ 
    return RedirectToAction("Edit", new {id = 1}); 
}