我有一个网络表单,我想在其中选择文件列表,输入一些附加信息并提交整个捆绑包。然后控制器应根据额外输入的数据处理文件列表。因此,我想将所有内容捆绑在一个模型中:
public class MyModel
{
public string AdditionalInformations { get; set; }
public int AdditionalInformationsId { get; set; }
public IEnumerable<HttpPostedFile> Files { get; set; }
}
public class MyController: Controller
{
[HttpPost]
public ActionResult UploadFile(MyModel Model)
{
switch(Model.AdditionalInformationsId)
{
case 1:
// do something with Model.Files
case 2:
// do something else with Model.Files
...
}
RedirectToAction("Index");
}
}
我的观点看起来像
@model MyProgram.Models.MyModel
@using MyProgram.Models
@using (Html.BeginForm("UploadFile", "MyController", FormMethod.Post,
new { enctype = "multipart/form-data" }))
{
<div class="row">
<div class="col-sm-6">
<input type="file" name="Files" multiple />
</div>
<div class="col-sm-6"></div>
</div>
<div class="row">
<div class="col-sm-3">
@Html.DropDownListFor(model => model.AdditionalInformationsId,
((MyDropDownList)Session["DDLists"]).companies,
Model.AdditionalInformation,
new { @class = "form-control", id = "ReceivingCompany" })
</div>
<div class="col-sm-3">
<br />
<button class="btn btn-default" type="submit">Upload</button>
</div>
<div class="col-sm-6"></div>
</div>
}
如果我现在使用提交按钮,模型将被发送到控制器。例如AdditionalInformationsId
,在表单中输入了值。但是IEnumerable Files
总是空的,除非我提交表单而不选择任何要上传的文件,在这种情况下,列表包含一个null
-object。将属性添加id="Files"
到多文件输入标签也不起作用。
我找不到错误。是否有我必须在 web.config 中设置的设置?