asp.net-mvc Asp.Net MVC Razor FileUpload Html Helper 到现有视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5723189/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Asp.Net MVC Razor FileUpload Html Helper into existing View
提问by MaticDiba
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:
我有一个视图,可以将产品添加到一个简单的 WebStore。我想要一个产品有多个图像。这就是我使用 Microsoft.Web.Helpers 的 FileUpload 的原因。我的View中FileUpload的使用是这样的:
@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:
问题是当我使用 post 方法时,我的控制器没有得到任何东西。我使用这样的控制器:
[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.
那么有没有人知道我做错了什么。因为我的 FileUpload 对象总是空的。
回答by Buildstarted
Most likely your BeginForm
is missing the required field. It should look like this:
您很可能BeginForm
缺少必填字段。它应该是这样的:
using(Html.BeginForm("Index", "Home", FormMethod.Post,
new { enctype="multipart/form-data" })) {
}
回答by Sender
Now using HttpPostedFileBase
in mvc 4 easy to upload multiple files, EditorFor FileUpload in asp.net mvc 4 razor
现在HttpPostedFileBase
在mvc 4中使用很容易上传多个文件,在asp.net mvc 4 razor中使用EditorFor FileUpload
回答by user3405201
I had this problem last night FileUpload
control and the parameter name should be the same
我昨晚FileUpload
控制有这个问题,参数名称应该是一样的
Change:
改变:
public ActionResult Create(Product product, IEnumerable<HttpPostedFileBase> **fileUpload**)
to
到
public ActionResult Create(Product product, IEnumerable<HttpPostedFileBase> **upload**)