JSON RedirecttoAction [英] JSON RedirecttoAction
问题描述
我试图从一个视图更改表中的值,然后使用Flash FSCommand和Json使用以下代码重定向到另一个视图:
I am trying to change a value in a table from one view, and then redirect to another view using Flash FSCommand and Json, using the following code:
if (command == "nameClip") {
var url = '<%= Url.Action("Index", "Home") %>';
var clip = [args];
try {
$.post(url, { MovieName: clip }, function(data) {
;
}, 'json');
}
finally {
// window.location.href = "/Demo/SWF";
}
}
在控制器中:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(SWF movietoplay)
{
var oldmovie = (from c in db.SWFs
where c.ID == "1"
select c).FirstOrDefault();
var data = Request.Form["MovieName"].ToString();
oldmovie.SWFName = data;
db.SubmitChanges();
return RedirectToAction("Show");
}
除重定向外,其他所有方法都正常!
All works well except Redirect!!
推荐答案
您需要在AJAX成功回调中执行重定向:
You need to perform the redirect inside the AJAX success callback:
$.post(url, { MovieName: clip }, function(data) {
window.location.href = '/home/show';
}, 'json');
无法在服务器端执行重定向,因为您正在使用AJAX调用此操作.
The redirect cannot be performed server side as you are calling this action with AJAX.
还要在AJAX调用中指出您希望服务器端提供JSON,但您发送的重定向不一致.您可以修改控制器操作,以简单地返回客户端需要使用JSON重定向到的网址:
Also you indicate in your AJAX call that you are expecting JSON from the server side but you are sending a redirect which is not consistent. You could modify the controller action to simply return the url that the client needs to redirect to using JSON:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(SWF movietoplay)
{
...
return Json(new { redirectTo = Url.Action("show") });
}
然后:
$.post(url, { MovieName: clip }, function(data) {
window.location.href = data.redirectTo;
}, 'json');
这篇关于JSON RedirecttoAction的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!