views:

27

answers:

1

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

A: 

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');
Darin Dimitrov
Thank you so much Darin, also thanks to the developer of this website; it works very well, just minor problem with IE, I get this error Windows Data Execution Prevention detected an add-on trying to use system memory incorrectly. This can be caused by a malfunction or a malicious add-on. Maybe because I am running the application on local server. Thanks again