ASP.NET MVC3 RAZOR:文件上载给出文件计数为零

我需要使用带有RAZOR的MVC3将多个文件上传到Web服务器.我有以下代码.在控制器中,我的文件数为零.如何更正它以获取上传文件的实际数量获取内容
public class MyFileController : Controller
{

    public ActionResult MyFileProcessActiontest()
    {
        return View();
    }

    [HttpPost]
    public ActionResult MyFileProcessActionTest(IEnumerable<System.Web.HttpPostedFileBase> files)
    {

        int fileCount = files.Count<System.Web.HttpPostedFileBase>();
        return RedirectToAction("Index");
    }
}

视图

@{
ViewBag.Title = "MyFileProcessActionTest";
}

<h2>MyFileProcessActionTest</h2>

@using (Html.BeginForm())
{

<input type="file" name="files" id="file1" />
<input type="file" name="files" id="file2" />

<input type="submit"  />

}

读:

> Binding HttpPostedFileBase using Ajax.BeginForm
> ASP.NET MVC上传和下载文件
http://www.mikesdotnetting.com/Article/125/ASP.NET-MVC-Uploading-and-Downloading-Files
> How do I Validate the File Type of a File Upload?
> MVC 3 file upload and model binding

解决方法

更改您的表单以匹配以下内容
@using(Html.BeginForm("action","controller",FormMethod.Post,new{encType = "multipart/form-data"})){
{

<input type="file" name="files[0]" id="file1" />
<input type="file" name="files[1]" id="file2" />
<input type="file" name="files[2]" id="file3" />

<input type="submit"  />

}

索引0,1,2将允许modelbinder绑定到IEnumerable,此外还必须在将文件发布到服务器时指定encType

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....