Asp.Net MVC Razor FileUpload Html Helper in Existing View

I have a view where I add products to a simple web store. I want to have several images with one product. Therefore, I use FileUpload from Microsoft.Web.Helpers. Using FileUpload in my view is as follows:

@FileUpload.GetHtml("Upload", 5, true, true, addText: "Add more", uploadText: "Upload files") 

Then I have some labels and fields for other product attributes, for example:

 <div class="editor-field"><br> @Html.EditorFor(model => model.Title)<br> @Html.ValidationMessageFor(model => model.Title)<br> </div> 

The problem is that when I use the post method, my controller does not receive anything. I use the controller as follows:

 [HttpPost] public ActionResult Create(Product product, IEnumerable<HttpPostedFileBase> fileUpload) { foreach (var file in fileUpload) { var fileName = Path.GetFileName(file.FileName); } return RedirectToAction("Index"); } 

So, does anyone know what I'm doing wrong. Since my FileUpload object is always empty.

+4
source share
3 answers

Most likely your BeginForm missing the required field. It should look like this:

 using(Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype="multipart/form-data" })) { } 
+14
source

Now, using HttpPostedFileBase in mvc 4, it's easy to upload multiple files, EditorFor FileUpload in asp.net mvc 4 razor

+5
source

I had this problem last night FileUpload and the parameter name should be the same

Edit:

 public ActionResult Create(Product product, IEnumerable<HttpPostedFileBase> **fileUpload**) 

to

 public ActionResult Create(Product product, IEnumerable<HttpPostedFileBase> **upload**) 
0
source

All Articles