开发者

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.


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


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

Change:

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

to

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜