2

I have form with file upload on default 'Index' view that calls action ('FileUpload') from the same controller ('Admin' in this example):

@using (Html.BeginForm("FileUpload", "Admin", FormMethod.Post, htmlAttributes: new { enctype = "multipart/form-data" }))
        {

            <label for="articleFile" class="control-label">Upload article</label>
            <input id="articleFile" name="articleFile" type="file" accept=".txt" />
            <button type="submit">Upload</button>
        }

When I navigate to this view URL looks like:

http://mydomain/admin

Inside FileUpload action I do upload logic, create model for 'Index' view, set some values in model (e.g. upload status message) and return ViewResult:

 [HttpPost]
    public ActionResult FileUpload(HttpPostedFileBase articleFile)
    {
        IndexModel model = new IndexModel();

        // file upload logic here

        model.FileUploadMessage = "message";

        return View("Index", model);
    }

It all works fine, file is uploaded and message is displayed but URL in browser changes to:

http://mydomain/admin/fileupload

So my question is: Is it possible to prevent it and make URL stay the same after form submit done that way, when ViewResult with action name Index and model is returned?

I know that I can use jQuery plugin for ajax async file upload or use RedirectToAction as result but then I am not be able to pass model, just route values.

PrzemG
  • 775
  • 6
  • 7
  • possible duplicate of [.NET MVC : Calling RedirectToAction passing a model?](http://stackoverflow.com/questions/2324044/net-mvc-calling-redirecttoaction-passing-a-model) – walther Feb 21 '15 at 17:34
  • Thanks, but I don't think that it is the same question. That one suggests to redirect to default action with id parameter passed in query string so updated model can be retrieved from data source as model can't be passed to RedirectToAction. I don't want pass model to RedirectToAction I just want URL to stay the same after form submit. – PrzemG Feb 21 '15 at 17:56
  • 1
    I understand, but there's no other way.. 1.) RedirectToAction with parameteres/tempdata/whatever, 2.) ajax, 3.) place your code into Index action..Once you post to an url, you have to redirect to get out of there "somehow". That's how http works. With submit you create a http post to that url, so it would be really weird if you just returned a different url if there wasn't any redirection. – walther Feb 21 '15 at 18:24
  • Yes, that makes sense. I went for option 3 and just renamed action from FileUpload to Index so I post to the same URL. I have 2 versions of Index action now - get and post - and now URL stays the same. Thanks for help! – PrzemG Feb 21 '15 at 18:46

1 Answers1

1

Redirecting your page to back to index is good opting

return RedirectToAction("Index", model);

Because it is good practice to follow Post/Redirect/Get.

If you choose not to follows this on refresh of your browser after submit request. Your request will again get posted and you might face some issue of uploading same file again or any miss behavior of system.

Dnyanesh
  • 2,265
  • 3
  • 20
  • 17
  • I can't pass model to RedirectToAction, just routeValues. – PrzemG Feb 21 '15 at 17:59
  • No problem I just thought you have some model associated with Index method. I just wanted to point that redirect page to Index method. Pass model if you have otherwise just redirect to Index method. – Dnyanesh Feb 21 '15 at 18:03