一直在尝试实现具有多个文件的文件上传器,但我一直收到无效信息ModelState
。这是我的代码(只是要点)
ViewModel:
public IEnumerable<HttpPostedFileBase> files { get; set; }
view.cshtml:
@model ITManagement.ViewModels.AssetViewModel
@using (Html.BeginForm(new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Asset</h4>
<hr />
.
. The rest of the form is located here
.
.
<div class="form-group">
@Html.LabelFor(viewModel => viewModel.objectModel.documentInfo.documents.fileContent, htmlAttributes: new {@class = "control-label col-md-2" } )
<div class="col-md-2">
<input class="single-line" [email protected](viewModel => viewModel.files)
[email protected](viewModel => viewModel.files)
type="file" multiple="multiple" />
</div>
</div>
控制器:
[Route("create", Name = AssetsControllerRoute.Create)]
public ActionResult create()
{
AssetViewModel evm = new AssetViewModel();
return View(evm);
}
// POST: Assets/Create
[HttpPost]
[ValidateAntiForgeryToken]
[Route("create")]
public ActionResult create(AssetViewModel asset)
{
if (ModelState.IsValid)
{
erepo.add(asset.objectModel);
return RedirectToAction("Index");
}
return View(asset);
}
我已经遇到了这个问题,然后又遇到了这个错误和答案,但是我仍然缺少一些东西。files
当我上传文件并提交表单时,该变量为空。我Request.Files
在搜索中确实看到了using ,但是尝试绑定到IEnumerable<HttpPostedFileBase>
和使用之间有什么区别Request.Files
?
这是无效的ModelState的原因:
“从类型'System.String'到类型'System.Web.HttpPostedFileBase'的参数转换失败,因为没有类型转换器可以在这些类型之间进行转换。”
我弄错的一段代码是这样的
@using (Html.BeginForm(new { enctype = "multipart/form-data" }))
这应该是
@using (Html.BeginForm("create", "assets", FormMethod.Post, new { enctype = "multipart/form-data" }))
“创建”代表控制器动作,“资产”代表控制器名称。我给了这三个参数,一切都很好。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句