C# 在ASP.NET MVC中,HttpPostedFileBase集合只有空项

C# 在ASP.NET MVC中,HttpPostedFileBase集合只有空项,c#,asp.net,.net,asp.net-mvc,file-upload,C#,Asp.net,.net,Asp.net Mvc,File Upload,这是我的代码: @using (Html.BeginForm("UploadImages", "Administration", new { _id = Model.Album.AlbumID, enctype = "multipart/form-data" }, FormMethod.Post)) { <input type="file" name="fileUpload" id="file1" /><br /> <input typ

这是我的代码:

@using (Html.BeginForm("UploadImages", "Administration", new { _id = Model.Album.AlbumID, enctype = "multipart/form-data" }, FormMethod.Post))
{ 
    <input type="file" name="fileUpload" id="file1" /><br />      
    <input type="file" name="fileUpload" id="file2" /><br />      
    <input type="file" name="fileUpload" id="file3" /><br /> 
    <input name="addPhoto" type="submit" value="Добавить фото" />
}



 [Authorize]
   [HttpPost]
   public ActionResult UploadImages(int _id, IEnumerable<HttpPostedFileBase> fileUpload)
        {
            gb_albumdbEntities1 entityes = new gb_albumdbEntities1();
            foreach (var file in fileUpload)
            {
                if (file == null) continue; // **<---fileUpload items is always null!**
                string path = AppDomain.CurrentDomain.BaseDirectory + "Photos/";
                if (Path.GetFileName(file.FileName) != null)
                {
                    string filename = file.GetHashCode().ToString();
                    string fullpath = Path.Combine(path, filename);
                    file.SaveAs(fullpath);
                    entityes.Photos.AddObject(new Photo() { AlbumID = _id, PhotoUrl = @"http://site.ru/Photos/" + filename });
                }
            }
            entityes.SaveChanges();
            return RedirectToAction("AlbumEdit", new { id = _id });
        }
@使用(Html.BeginForm(“UploadImages”,“Administration”,new{{u id=Model.Album.AlbumID,enctype=“multipart/form data”},FormMethod.Post))
{ 



} [授权] [HttpPost] public ActionResult UploadImages(int\u id,IEnumerable fileUpload) { gb_albumdbEntities1 entityes=新的gb_albumdbEntities1(); foreach(fileUpload中的var文件) {
如果(file==null)continue;//**如果模型绑定需要对文件输入进行编号,则列表将继续。以最简单的形式,视图应如下所示:

<html>
<head>
    <title>Index</title>
</head>
<body>
    <div>
        @using (Html.BeginForm("Index", "Upload", FormMethod.Post, new { enctype = "multipart/form-data" }))
        {            
            for (int i = 0; i < 3; i++)
            {
                @Html.TextBox(string.Format("fileUpload[{0}]", i), null, new { type="file" })<br />
            }
            <input name="submit" type="submit" value="Go" />
        }
    </div>
</body>
</html>

指数
@使用(Html.BeginForm(“Index”,“Upload”,FormMethod.Post,new{enctype=“multipart/formdata”}))
{            
对于(int i=0;i<3;i++)
{
@Html.TextBox(string.Format(“fileUpload[{0}]”,i),null,new{type=“file”})
} }
和您的控制器:

    public class UploadController : Controller
    {
        //
        // GET: /Upload/

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

        [HttpPost]
        public ActionResult Index(IEnumerable<HttpPostedFileBase> fileUpload)
        {

            return View();
        }

    }
公共类上载控制器:控制器
{
//
//获取/上传/
公共行动结果索引()
{
返回视图();
}
[HttpPost]
公共操作结果索引(IEnumerable fileUpload)
{
返回视图();
}
}

试试我以前用过的以下帖子。为我工作


我做了这个,但没有任何改变。它对我有效……可能是你的Html.BeginForm语句,因为你最后有FormMethod.Post值。使用(Html.BeginForm(“UploadImages”,“Administration”,FormMethod.Post,new{enctype=“multipart/form data”})尝试>>>@