Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON RedirecttoAction

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";
  }
}

In the controller:

[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!!

like image 285
hncl Avatar asked Mar 21 '26 09:03

hncl


1 Answers

You need to perform the redirect inside the AJAX success callback:

$.post(url, { MovieName: clip }, function(data) {
    window.location.href = '/home/show';
}, 'json');

The redirect cannot be performed server side as you are calling this action with AJAX.

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") });
}

and then:

$.post(url, { MovieName: clip }, function(data) {
    window.location.href = data.redirectTo;
}, 'json');
like image 70
Darin Dimitrov Avatar answered Mar 25 '26 00:03

Darin Dimitrov