MVC3重定向到行动Ajax调用后 [英] MVC3 redirect to action after ajax call
问题描述
在一个ASP.NET MVC3应用程序我在视图中的一个按钮。
In an ASP.NET MVC3 Application I have a button in the view.
在单击该按钮调用一个函数,它的jQuery AJAX调用,以项目保存到数据库中。
When the button is clicked a function is called and it jquery ajax call is made to save items to the database
function SaveMenuItems() {
var encodeditems = $.toJSON(ids);;
$.ajax({
type: 'POST',
url: '@Url.Action("SaveItems", "Store")',
data: 'items=' + encodeditems + '&storeKey=@Model.StoreID',
complete: function () {
}
}
});
}
我想以后的项目将被保存到我要重定向到另一个视图数据库。 (转发到动作)
What i want is after the items are saved to the database I want to redirect to another view. (Redirect to action)
我怎么能这样做?
我试图用返回RedirectToAction(存储,存储)
在年底控制器中的 SaveItems
功能。但它不工作
I tried to use return RedirectToAction("Stores","Store")
in the controller at the end of the SaveItems
function. But it is not working
我也尝试添加 window.location.replace(/存储/存储);
在Ajax调用的完整功能,但也不能工作
I also tried to add window.location.replace("/Store/Stores");
in the complete function of the ajax call but didn't work either
任何帮助是极大AP preciated
Any help is greatly appreciated
非常感谢
推荐答案
您可以使用JavaScript重定向到新的一页。 window.location.href
的值设置为你的Ajax调用的成功的新网址
/ 完整
事件。
You can use javascript to redirect to the new page. Set the value of window.location.href
to the new url in your ajax call's success
/complete
event.
var saveUrl = '@Url.Action("SaveItems","Store")';
var newUrl= '@Url.Action("Stores","Store")';
$.ajax({
type: 'POST',
url: saveUrl,
// Some params omitted
success: function(res) {
window.location.href = newUrl;
},
error: function() {
alert('The worst error happened!');
}
});
或者在完成
事件
$.ajax({
url: someVariableWhichStoresTheValidUrl
}).done(function (r) {
window.location.href = '@Url.Action("Stores","Store")';
});
以上code使用 Url.Action
helper方法建立正确的相对URL的操作方法。如果您的JavaScript code是外部JavaScript文件中,你应该建立链接到应用程序根并传递给脚本/ $内外部JS文件C $ c和使用,要建立的URL action方法为在这个帖子。
The above code is using the Url.Action
helper method to build the correct relative url to the action method. If your javascript code is inside an external javascript file, you should build the url to the app root and pass that to your script/code inside external js files and use that to build the url to the action methods as explained in this post.
传递参数?
如果您想查询字符串某些参数传递到新的URL,可以使用<一个href=\"https://msdn.microsoft.com/en-us/library/system.web.mvc.urlhelper.action(v=vs.118).aspx#M:System.Web.Mvc.UrlHelper.Action%28System.String,System.String,System.Object%29\"相对=nofollow>在 Url.Action
方法的此重载它接受routevalues以及建立与查询字符串的URL。
If you want to pass some querystring parameters to the new url, you can use this overload of the Url.Action
method which accepts routevalues as well to build the url with the querystring.
var newUrl = '@Url.Action("Stores","Store", new { productId=2, categoryId=5 })';
,其中2和5可以与其他一些实际值替换。
where 2 and 5 can be replaced with some other real values.
由于这是一个html的辅助方法,它会在你的Razor视图只工作,不外部JS文件。如果你的code是内外部的js文件,你需要手动构建URL查询字符串参数。
Since this is an html helper method, It will work in your razor view only,not in external js files. If your code is inside external js file, you need to manually build the url querystring parameters.
这篇关于MVC3重定向到行动Ajax调用后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!