hncl
hncl

Reputation: 2295

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

Upvotes: 3

Views: 5510

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038720

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');

Upvotes: 2

Related Questions