0

In jQuery I have the following function:

function submitInventoryModal(unitFinancialCalendarId) {
    const invoiceForm = $('form#submittalForm');
    const formData = invoiceForm.serialize();
    $.post('/Inventory/SaveInventorySubmittal', formData)
        .success(function (response) {
            debugger;
            console.log(response);
            viewSubmitConfirmation(unitFinancialCalendarId);
            removeNavigationModalRequirement();
        });
}

In SaveInventorySubmittal the code has been implemented as:

[HttpPost]
[Authorize]
public async Task<IActionResult> SaveInventorySubmittal(InventorySubmittalBE inventorySubmittal)
{
    try
    {
     
        RsiAsyncResult saveResult = await _inventoryService.SaveInventorySubmittalAsync(inventorySubmittal);
        if (!saveResult.Succeeded)
            return RedirectToAction("Message", Strings.Home,
                new { Heading = "Error Saving Inventory Submittal", Message = "Invalid GL Configuration", SubMessage = saveResult.GetErrorString(), Icon = "user-times" });
        Logger.LogInformation($"Inventory Submittal with UnitInventorySubmittalId: {inventorySubmittal.UnitInventorySubmittalId} saved successfully.");
        return RedirectToAction("Index");
    }
    catch (Exception ex)
    {
        LogException(ex);
        return RedirectToException(ex);
    }
}

In this above method SaveInventorySubmittal, an exception is thrown from the _inventoryService, but the catch blocks return the RedirectToExceptiion, which doesn't throw the exception and also is supposed to be redirected to the error page (Message view). But that doesn't happen.

The RedirectToException method is implemented as:

protected RedirectToActionResult RedirectToException(Exception exception)
{
    LogException(exception);

    // If it's an RSI Exception, display the message
    RsiException rsiException = exception as RsiException;

    if (rsiException != null)
    {
        return RedirectToAction("Message", Strings.Home, new
        {
            Heading = "Error",
            Message = rsiException.DisplayMessage,
            SubMessage = ErrorSubMessage,
            Icon = "bomb"
        });
    }

    // For non-RSI exceptions, display something generic
    return RedirectToAction("Message", Strings.Home, new
    {
        Heading = "Error",
        Message = "We're sorry. An unexpected error has occurred.",
        SubMessage = ErrorSubMessage,
        Icon = "bomb"
    });

}

Due to this implementation, the redirects never happens from the submitInventoryModal in jQuery.

I wanted to redirect to the "Message" view, but the ajax code just goes into its own flow. How should I do the redirect?

freedomn-m
  • 27,664
  • 8
  • 35
  • 57
Rasik
  • 1,961
  • 3
  • 35
  • 72
  • Existing question that may answer your question: https://stackoverflow.com/questions/13129694/what-happens-when-an-ajax-request-gets-a-302-moved-response – freedomn-m Jan 06 '23 at 06:35
  • Another, more detailed answer: https://stackoverflow.com/questions/199099/how-to-manage-a-redirect-request-after-a-jquery-ajax-call – freedomn-m Jan 06 '23 at 06:37

1 Answers1

0

No, You can not redirect at the Controller side as you are using AJAX.POST. You will have to return JSON.

 catch(Exception ex)
  {
    LogException(ex);
   return Json(new { status="error",message="error in details"});
  }

Client-side:

  $.post('/Inventory/SaveInventorySubmittal', formData)
        .success(function (response) {
            debugger;
            console.log(response);
            viewSubmitConfirmation(unitFinancialCalendarId);
            removeNavigationModalRequirement();
            if(response.status=="error"){
              window.location.href = 'on error page'; 
            }
        })
Ajay Gupta
  • 703
  • 5
  • 11
  • exception may occurs from the multiple sources and you may need to use the same method on multiple instances, and returning that exception may not be a help. – Rasik Jan 06 '23 at 06:24
  • Yes, else you will have to use the form POST method then your RedirectToAction will work. – Ajay Gupta Jan 06 '23 at 06:27