I have have a View where I add products to a simple WebStore. I want to have multiple images with one product. That's why I use FileUpload from Microsoft.Web.Helpers. The use of FileUpload in my View is like this:
@FileUpload.GetHtml("Upload", 5, true, true, addText: "Add more", uploadText: "Upload files")
Then I have some labels and fields for other product attributs like this:
<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 post method, my controller does not get anything. I use controller like this:
[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 have any idea what I am doing wrong. Because my FileUpload object is always empty.
Most likely your BeginForm
is missing the required field. It should look like this:
using(Html.BeginForm("Index", "Home", FormMethod.Post,
new { enctype="multipart/form-data" })) {
}
Now using HttpPostedFileBase
in mvc 4 easy to upload multiple files, EditorFor FileUpload in asp.net mvc 4 razor
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With