Ciel
Ciel

Reputation: 17752

JSON Redirect from MVC Controller not working

I've tried the steps outlined in other posts here and here and it just doesn't work. I end up getting redirected to a white screen that just says ... {"redirectTo":"/Administrator/Home"}

C#

    [HttpPost]
    public JsonResult ControllerMethodHere(ViewModel model) {
        // my controller code goes here.
        return Json(new {
            redirectTo = Url.Action("Index", "Home"),
        }, JsonRequestBehavior.AllowGet);
    }

Javascript

    this.save = function () {

            $.ajax({
                url: $('form').attr('action'),
                type: "POST",
                data: ko.toJSON(this),
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                    window.location.href = data.redirectTo;
                }
            });
    };

Upvotes: 0

Views: 5283

Answers (1)

Paul
Paul

Reputation: 12440

Try using this:

window.location = data.redirectTo;

Upvotes: 6

Related Questions