RedirectToAction not changing URL or navigating to Index view

Solution:

The code you show is correct. My wild guess is that you're not doing a standard POST (e.g., redirects don't work with an AJAX post).

The browser will ignore a redirect response to an AJAX POST. It's up to you to redirect in script if you need to redirect when an AJAX call returns a redirect response.


You need to add "return false;" to end of your onclick javascript event. For example:

Razor Code

@using (Html.BeginForm())
{
    @Html.HiddenFor(model => model.ID) 
    @Html.ActionLink("Save", "SaveAction", "MainController", null, new { @class = "saveButton", onclick = "return false;" })
}

JQuery Code

$(document).ready(function () {
        $('.saveButton').click(function () {
            $(this).closest('form')[0].submit();
        });
    });

C#

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult SaveAction(SaveViewModel model)
{
    return RedirectToAction("Index");
}

Try as below:

return in your action:

return Json(Url.Action("Index", "Home"));

and in your ajax:

window.location.href