Search code examples
asp.net-mvcfiddler

RedirectToAction redirecting to wrong place


I have an ASP.Net MVC website developed with VS2015. I have a controller called EditBatchController. This had a "Resubmit" method that is posted to from a Javascript Ajax call.

Here's the call:

self.resubmit = function () {
    var data = { Recs: ko.toJS(this.recs) };

    $.ajax({
        type: "POST",
        url: BASE_URL + 'EditBatch/Resubmit',
        data: ko.toJSON(data),
        contentType: 'application/json',
        async: true,
        success: function (data) {
            window.location = BASE_URL + 'EditBatch/Index';
        },
        error: function (data) {
            toastrs(false);
        }
    });

And here's the signature of the controller's resubmit method:

[HttpPost]
//Called by the Knockout View Model to update db
public void Resubmit(List<EditBatchViewModel>recs)
{

At the end of this method, I would like to redirect to the "Index" action of a different controller, so I have the following:

RedirectToAction(actionName: "Index", controllerName: "APInvoicesSummaryController");

My problem is, on executing this line, a GET request for EditBatch/Index is issued rather than for APInvoicesSummaryController/Index - this is shown in the following Fiddler screen grab:

fiddler

I have used RedirectToAction many times previously with success but this time I am stumped. Can anyone please tell me where I went wrong?


Solution

  • Doh! As soon as I posted then I could see my mistake. The redirect to "EditBatch" is coming from the Javascript -

    success: function (data) {
                    window.location = BASE_URL + 'EditBatch/Index';
    

    Hope this helps somebody else as tired as I am in future, possibly even me - not uncommon!