RedirectToAction with Ajax.Beginform , unexpected results
Asked Answered
T

2

8

I have the following view , which contains an Ajax.BeginForm:-

@using (Ajax.BeginForm("ChangeDevicesSwitch", "Switch", new AjaxOptions

{
    InsertionMode = InsertionMode.InsertBefore,
    UpdateTargetId = "result",
    LoadingElementId = "progress2",
    HttpMethod= "POST"
    ,
    OnSuccess = "createsuccess",
    OnFailure = "createfail"




}))
//code goes here
<p><img src="~/Content/Ajax-loader-bar.gif" class="loadingimage" id="progress2" /></p>
<div id ="result"></div>

and the following Action Method which will be called from the Ajax.Bginform:-

public ActionResult ChangeDevicesSwitch(SwitchJoin s)

        {//code goes here
            try
            {
                var count = repository.changeDeviceSwitch(s.Switch.SwitchID, (Int32)s.GeneralSwitchTo, User.Identity.Name.Substring(User.Identity.Name.IndexOf("\\") + 1));
                repository.Save();
                return RedirectToAction("Details", new { id = s.GeneralSwitchTo });

            }
            catch (Exception e)

            {
                return Json(new { IsSuccess = "custome", description = "Error occurred. Please check...." }, JsonRequestBehavior.AllowGet);
            }



        }

The script which will run when the Ajax.BeginForm return success is :-

function createsuccess(data) {
    if (data.IsSuccess == "Unauthorized") {

        jAlert(data.description, 'Unauthorized Access');
    }
    else if (data.IsSuccess == "False") {

        jAlert('Error Occurred. ' + data.description, 'Error');
    }
    else if (data.IsSuccess == "custome") {

        alert(data.description);

    }
    else  {
        jAlert('Record added Successfully ', 'Creation Confirmation');
    }

}

currently i am facing a problem is that when the RedirectToAction is reach ,, the whole view will be displayed inside the current view!! so is there a way to force my application not to update the target if an RedirecttoAction is returned ?

Teilo answered 21/5, 2014 at 14:55 Comment(2)
You can not make a redirect via ajax. Instead you can send the redirect url back to the client and on createsuccess function set the window.location to the provided url.Cannabin
can you advice more on this with a sample codeTeilo
C
18

Return the url you want to make a redirect to from the action method when the operation succeeded:

public ActionResult ChangeDevicesSwitch(SwitchJoin s)
{
    try
    {
        ...
        return Json(new { RedirectUrl = Url.Action("Details", new { id = s.GeneralSwitchTo }) });
    }
    ...
}

And in the createsuccess:

function createsuccess(data) {
    if (data.RedirectUrl)
        window.location.href = data.RedirectUrl;
}
Cannabin answered 21/5, 2014 at 15:34 Comment(1)
here is one liner return JavaScript( "window.location = '" + Url.Action("Home","Details") + "'" ) check this, hope helps someone.Margarethe
S
0

in my case, following method was worked

function OnSuccess(data) {
    var json;
        if (data.responseText instanceof Object)
            json = data.responseText;
        else
            json = $.parseJSON(data.responseText);

        if (json.RedirectUrl)
            window.location.href = json.RedirectUrl;
}
Shuntwound answered 29/5, 2017 at 16:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.