RedirectToAction不会更改URL或导航到索引视图 [英] RedirectToAction not changing URL or navigating to Index view
问题描述
在向控制器发布并保存后,我尝试使用RedirectToAction
,但URL不会更改,并且重定向似乎不起作用.我需要添加的是,调试时重定向确实会进入控制器操作方法.它不会更改URL或导航到Index
视图.
I tried using a RedirectToAction
after I have done a post to the controller and saved but the URL does not change and the redirect does not seem to work. I need to add that the redirect does enter the controller action method when I debug. It does not change the URL or navigate to the Index
view.
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调用:
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);
};
推荐答案
您显示的代码正确.我的疯狂猜测是您没有执行标准的POST(例如,重定向不适用于AJAX帖子).
The code you show is correct. My wild guess is that you're not doing a standard POST (e.g., redirects don't work with an AJAX post).
浏览器将忽略对AJAX POST的重定向响应.如果需要在AJAX调用返回重定向响应时进行重定向,则由脚本来重定向.
The browser will ignore a redirect response to an AJAX POST. It's up to you to redirect in script if you need to redirect when an AJAX call returns a redirect response.
这篇关于RedirectToAction不会更改URL或导航到索引视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!