Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.Net MVC Razor FileUpload Html Helper into existing View

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.

like image 884
MaticDiba Avatar asked Dec 01 '22 02:12

MaticDiba


2 Answers

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" })) { 

}
like image 114
Buildstarted Avatar answered Dec 04 '22 11:12

Buildstarted


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

like image 39
Sender Avatar answered Dec 04 '22 10:12

Sender