Reputation: 795
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:
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:
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.
Upvotes: 3
Views: 3331
Reputation: 2343
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.
Upvotes: 2