上传的一种形式MVC4多个文件 [英] Upload multiple files in one form MVC4

查看:135
本文介绍了上传的一种形式MVC4多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一种形式

@using (Html.BeginForm("Create", "AdminRestaurants", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div class="form-group">
    <label for="logoFile" class="col-sm-2 control-label">Logo:</label>
    <div class="col-sm-6">
        <input type="file" multiple="" name="logoFile" id="logoFile" />
    </div>
</div>
<div class="form-group">
    <label for="fohFile" class="col-sm-2 control-label">FOH Logo:</label>
    <div class="col-sm-6">
        <input type="file" multiple="" name="fohFile" id="fohFile" />
    </div>
</div>
<div class="form-group">
    <label for="bohFile" class="col-sm-2 control-label">BOH Logo:</label>
    <div class="col-sm-6">
        <input type="file" multiple="" name="bohFile" id="bohFile" />
    </div>
</div>
<div class="form-group">
    <label for="mgmFile" class="col-sm-2 control-label">MGM Logo:</label>
    <div class="col-sm-6">
        <input type="file" multiple="" name="mgmFile" id="mgmFile" />
    </div>
</div>

我想处理与此控制器

I'm trying to process the form on the controller with this

public ActionResult Create(IEnumerable<HttpPostedFileBase> files, RestaurantModel collection)
{
    if (ViewData.ModelState.IsValid)
    {
    }
}

目前什么也不显示控制器上的文件签名。这似乎工作的伟大,当只有一个文件的工作。

Currently nothing shows up in the files signature on the controller. This seems to work great when only working with one file

public ActionResult Create(HttpPostedFileBase file, EventsModel collection)

有人点我的方向,允许多个文件与一个提交表单上传?

Can someone point me in the direction to allow multiple files to be uploaded with one submission form?

推荐答案

您的问题是,你形成创建与模型粘结剂可以绑定因为命名约定是不正确的信息后请求。

Your problem is that you form creates a post request with information that the model binder can bind because the naming convention is not right.

你看,你有每个4文件中的字段使用不同的名称,模型绑定给他们正确绑定你的控制器操作的签名应该是这样的:

you see, you have 4 file fields each with a different name, for the model binder to bind them correctly your controller action signature should look like this:

public ActionResult Create(HttpPostedFileBase mgmFile, 
                           HttpPostedFileBase logoFile, 
                           HttpPostedFileBase fohFile ,
                           HttpPostedFileBase bohFile)

随着MCV的设计模式最好的办法是使用持有的IEnumerable℃的视图模型; HttpPostedFileBase&GT;
你可以创建一个的IEnumerable℃的自定义编辑器模板; HttpPostedFileBase&GT;

这样你可以使用它像:

Html.EditorFor(m=>Model.filesUploaded)

和您的控制器动作是这样的:

and your controller action would look like this:

public ActionResult Create(MyViewModel i_InputModel)
{
    i_InputModel.filesUploade; //Use the files here to upload them.
}

其他选项有:
使用该文件输入字段中的HTML5多属性如下:

Other options are: Use the HTML5 multiple attribute on the file input field like this:

<label for="mgmFile" class="col-sm-2 control-label">Files:</label>
<div class="col-sm-6">
    <input type="file" multiple="multiple" name="files" id="files" />
</div>

和这样的控制器动作:

public ActionResult Create(HttpPostedFileBase files)

或使用多个文件中的字段,但指数在它们的名称:

or use multiple file fields but index them in their name:

<input type="file" multiple="multiple" name="files[0]" id="files_1" />
<input type="file" multiple="multiple" name="files[1]" id="files_2" />
<input type="file" multiple="multiple" name="files[2]" id="files_3" />
<input type="file" multiple="multiple" name="files[3]" id="files_4" />

然后你可以使用这样的控制器动作:

and then you could use a controller action like this:

public ActionResult Create(IEnumerable<HttpPostedFileBase> files)

这篇关于上传的一种形式MVC4多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆