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

ASP.NET MVC3 RAZOR:文件上载的文件计数为零,asp.net,.net,asp.net-mvc,razor,Asp.net,.net,Asp.net Mvc,Razor,我需要上传多个文件到web服务器使用MVC3与剃须刀。我有以下代码。在控制器中,文件计数为零。如何更正它以获得上传文件的实际数量和内容 public class MyFileController : Controller { public ActionResult MyFileProcessActionTest() { return View(); } [HttpPost] public ActionResult MyFileProce

我需要上传多个文件到web服务器使用MVC3与剃须刀。我有以下代码。在控制器中,文件计数为零。如何更正它以获得上传文件的实际数量和内容

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");
    }
}
看法

阅读:

ASP.NET MVC上载和下载文件

必须在表单标记中包含enctype属性,以指示表单应包含文件

@using (Html.BeginForm("YourAction", "Controller", FormMethod.Post, new {enctype="multipart/form-data"))
{
}
必须在表单标记中包含enctype属性,以指示表单应包含文件

@using (Html.BeginForm("YourAction", "Controller", FormMethod.Post, new {enctype="multipart/form-data"))
{
}

更改您的表单以匹配以下内容

@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,并且在将文件发布到服务器时,还必须指定索引类型

@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

对于包含文件上载的表单,人们经常会错过encType=多部分/表单数据。我相信我们无法使用GET方法发布文件,因为我将文件获取为null,并且Request.files.Count也为0,如果表单是AjaxForm并且也有RouteValue,会有什么区别吗?@bjan您不能用ajax以这种方式上传文件。。。你应该在谷歌上搜索如何使用iFrame通过ajax上传文件。对于包含上传文件的表单,人们经常会错过enctype=multipart/form数据。我相信我们不能使用GET方法发布文件。我将文件设置为null,并且Request.files.Count也为0,如果表单是AjaxForm并且也有RouteValue,会有什么区别吗?@bjan您不能用ajax以这种方式上传文件。。。您应该在google上搜索如何使用iFramework使用ajax上传文件。成功了@使用Html.BeginFormMyFileProcessActionTest、MyFile、FormMethod.Post、new{enctype=multipart/form data}{我得到的文件为null,Request.Files.Count也为0,如果表单是AjaxForm,并且也有RouteValue,会有什么区别吗?bjan,我没有用ajax测试过它,但我相信它甚至可以用ajax工作。@bjan你不能用ajax上传文件……你应该在google上搜索如何使用ajax上传文件IframeThank.Worked。@使用Html.BeginFormMyFileProcessActionTest、MyFile、FormMethod.Post、new{enctype=multipart/form data}{我得到的文件为null,Request.Files.Count也为0,如果表单是AjaxForm,并且也有RouteValue,会有什么区别吗?bjan,我没有用ajax测试过它,但我相信它甚至可以用ajax工作。@bjan你不能用ajax上传文件……你应该在google上搜索如何使用ajax上传文件iframe