ASP.MVC 5 的上传文件列表为空
List of uploaded files with ASP.MVC 5 is empty
我有一个网络表单,我想在其中 select 文件列表,输入一些附加信息并提交整个包。然后,控制器应根据额外输入的数据处理文件列表。因此,我想将所有内容捆绑在一个模型中:
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?
中是否有我必须设置的设置?
将您的 属性 类型更改为 HttpPostedFileBase
的 IEnumerable
public IEnumerable<HttpPostedFileBase> Files { get; set; }
现在发布的文件将映射到您的 HttpPost 操作中的文件 属性。
我有一个网络表单,我想在其中 select 文件列表,输入一些附加信息并提交整个包。然后,控制器应根据额外输入的数据处理文件列表。因此,我想将所有内容捆绑在一个模型中:
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?
中是否有我必须设置的设置?将您的 属性 类型更改为 HttpPostedFileBase
public IEnumerable<HttpPostedFileBase> Files { get; set; }
现在发布的文件将映射到您的 HttpPost 操作中的文件 属性。