首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >HttpPostedFileBase总是空的

HttpPostedFileBase总是空的
EN

Stack Overflow用户
提问于 2014-12-24 06:58:31
回答 2查看 1.4K关注 0票数 0

我试图从我的表单上传图像文件,以及我的模型中的其他字段。我的HttpPostedFileBase集合总是空的,计数为0。

我在其中提到了许多其他与此有关的问题,但不知何故,我无法找到解决办法。

视图:

代码语言:javascript
复制
  @using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" })) 
  {
    @Html.AntiForgeryToken()

    <div class="form-horizontal">

        @Html.ValidationSummary(true)

        <div class="form-group">
            @Html.LabelFor(model => model.Id, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Id)
                @Html.ValidationMessageFor(model => model.Id)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.ProfileId, "ProfileId", new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("ProfileId", String.Empty)
                @Html.ValidationMessageFor(model => model.ProfileId)
            </div>
        </div>


        <div class="form-group">
            @Html.LabelFor(model => model.Image1, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                <input name="fileuploadImage1" type="file" />
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Image2, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                <input name="fileuploadImage2" type="file" />
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Image3, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                <input name="fileuploadImage3" type="file" />
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Image4, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                <input name="fileuploadImage4" type="file" />
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Image5, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                <input name="fileuploadImage5" type="file" />
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

控制器:

代码语言:javascript
复制
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "Id,ProfileId,fileuploadImage1,fileuploadImage2,fileuploadImage3,fileuploadImage4,fileuploadImage5,Files")] HomepageSetting homepagesetting)
    {
        if (ModelState.IsValid)
        {
            try
            {
                List<String> imagesFilenames = new List<String>();
                /*Lopp for multiple files*/
                foreach (HttpPostedFileBase file in homepagesetting.Files)
                {
                    /*Geting the file name*/
                    string filename = System.IO.Path.GetFileName(file.FileName);
                    /*Saving the file in server folder*/
                    file.SaveAs(Server.MapPath("~/Images/" + filename));
                    string filepathtosave = "Images/" + filename;
                    imagesFilenames.Add(filepathtosave);
                }


                if(imagesFilenames.Count == 1)
                {
                    homepagesetting.Image1 = imagesFilenames[0];
                }
                else if (imagesFilenames.Count == 2)
                {
                    homepagesetting.Image1 = imagesFilenames[0];
                    homepagesetting.Image2 = imagesFilenames[1];
                }
                else if (imagesFilenames.Count == 3)
                {
                    homepagesetting.Image1 = imagesFilenames[0];
                    homepagesetting.Image2 = imagesFilenames[1];
                    homepagesetting.Image3 = imagesFilenames[2];

                }
                else if (imagesFilenames.Count == 4)
                {
                    homepagesetting.Image1 = imagesFilenames[0];
                    homepagesetting.Image2 = imagesFilenames[1];
                    homepagesetting.Image3 = imagesFilenames[2];
                    homepagesetting.Image4 = imagesFilenames[3];

                }
                else if (imagesFilenames.Count == 5)
                {
                    homepagesetting.Image1 = imagesFilenames[0];
                    homepagesetting.Image2 = imagesFilenames[1];
                    homepagesetting.Image3 = imagesFilenames[2];
                    homepagesetting.Image4 = imagesFilenames[3];
                    homepagesetting.Image5 = imagesFilenames[4];
                }                    

                ViewBag.Message = "File Uploaded successfully.";
            }
            catch
            {
                ViewBag.Message = "Error while uploading the files.";
            }
            db.HomepageSettings.Add(homepagesetting);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        ViewBag.ProfileId = new SelectList(db.Profiles, "Id", "name", homepagesetting.ProfileId);
        return View(homepagesetting);
    }

模型:

代码语言:javascript
复制
public partial class HomepageSetting
{
    public int Id { get; set; }
    //other  model properties

    public string Image1 { get; set; }
    public string Image2 { get; set; }
    public string Image3 { get; set; }
    public string Image4 { get; set; }
    public string Image5 { get; set; }

    public virtual Profile Profile { get; set; }
    public List<HttpPostedFileBase> Files { get; set; }
    public HomepageSetting()
    {
        Files = new List<HttpPostedFileBase>();
    }
}

有谁能告诉我我在这里做错了什么吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-12-24 07:18:09

没有用foreach循环这样做,它发生在foreach上,因为我也面临着这个问题:

代码语言:javascript
复制
for (int i = 0; i < Request.Files.Count; i++)
{
   HttpPostedFileBase myFile = Request.Files[i];
}
票数 2
EN

Stack Overflow用户

发布于 2014-12-24 07:27:58

在MVC中,为了工作它的默认绑定,您总是必须使用html元素的正确名称。在您的示例中,fileupload控件具有类似于fileuploadImage1、fileuploadImage2的名称,并且它不存在于您的模型中,因此它不是绑定。

我建议您将所有文件上传元素名称命名为。

代码语言:javascript
复制
@using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" })) 
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">

        @Html.ValidationSummary(true)

        <div class="form-group">
            @Html.LabelFor(model => model.Id, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Id)
                @Html.ValidationMessageFor(model => model.Id)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.ProfileId, "ProfileId", new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("ProfileId", String.Empty)
                @Html.ValidationMessageFor(model => model.ProfileId)
            </div>
        </div>


        <div class="form-group">
            @Html.LabelFor(model => model.Image1, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                <input name="files" type="file" />
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Image2, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                <input name="files" type="file" />
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Image3, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                <input name="files" type="file" />
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Image4, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                <input name="files" type="file" />
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Image5, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                <input name="files" type="file" />
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

你必须做的事。

代码语言:javascript
复制
[HttpPost]
public ActionResult View1([Bind(Include = "Id,ProfileId,fileuploadImage1,fileuploadImage2,fileuploadImage3,fileuploadImage4,fileuploadImage5,Files")] HomepageSetting homepagesetting)
        {
            for (int i = 0; i < homepagesetting.Files.Count; i++)
            {
                if (homepagesetting.Files[i] != null)
                {
                }
            }
            return View();
        }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27632843

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档