What is best practice for using RedirectToAction with Ajax Forms?

Coreysan 1,786 Reputation points
2022-06-07T19:50:33.567+00:00

So I just learned that when I submit a POST request using ajax form and formdata,
the controller doesn't respond to RedirectToAction(nameof(Index)).

So my solution was to put @URL .Action in the success method of the Ajax code:

    function Upload() {  
    $.ajax({  
    type: "POST",  
    url: "@(Url.Action("Create", "Inventory"))",  
    data: formData,  
    enctype: "multipart/form-data",  
    processData: false,  
    contentType: false,  
    success: function (response) {  
        window.location.href = '@Url.Action("Index", "Inventory")'  
        }  
    });  

Is this a best practice, or is there a better way to do this?

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,737 questions
{count} votes

Accepted answer
  1. Rena Ni - MSFT 2,066 Reputation points
    2022-06-09T02:03:29.83+00:00

    Hi @Coreysan ,

    Here is a working demo which combine Dropzone.js with form submit below.

    Model

    public class TestViewModel  
    {  
        public string Description { get; set; }  
        public List<IFormFile> File { get; set; }  
    }  
    

    View

    @model TestViewModel  
          
    <form asp-action="Test" asp-controller="Home" method="post" enctype="multipart/form-data" class="dropzone dz-clickable form-horizontal form-bordered" id="dropzoneForm">  
            <div>  
                <input asp-for="Description" />  
            </div>  
            <div class="form-group form-actions">  
            <div class="col-md-9 col-md-offset-4">  
                <button type="submit" id="submit" class="btn btn-sm btn-primary"><i class="fa fa-floppy-o"></i> Upload</button>  
            </div>  
          </div>  
     </form>  
    

    JS:
    209682-screenshot-2022-06-09-095832.png

    Controller

    [HttpPost]  
    public async Task<ActionResult> Test(TestViewModel model)  
    {  
        //do your sufff...  
        return View();  
    }  
    

    Note

    You can get the js code by downloading the txt file here


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best Regards,
    Rena

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 69,426 Reputation points
    2022-06-08T15:40:12.467+00:00

    a better approach if for the server to return the redirect url as part of a standard response

        {   
            "success": true,   
            "data": null,   
            "redirectUrl": "",   
            "errorList":[]   
        }   
       
    
    0 comments No comments

  2. Coreysan 1,786 Reputation points
    2022-06-10T17:28:45.757+00:00

    I learned that no matter how many input controls there are on a view, I can get all the files by
    doing this in the controller:

    var files = HttpContext.Request.Form.Files;

    That provided the smartest solution for me!

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.