簡體   English   中英

HttpPostedFileBase始終為空

[英]HttpPostedFileBase is always empty

我正在嘗試從表單以及模型中的其他字段上載圖像文件。 我的HttpPostedFileBase集合始終為空,計數為0。

我在SO中還提到了與此有關的許多其他問題,但是不知何故我找不到解決方案。

視圖:

  @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>
}

控制器:

    [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);
    }

模型:

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>();
    }
}

誰能指出我在這里做錯了什么?

而不是這樣的foreach循環,它與foreach一起發生,因為我也遇到了這個問題:

for (int i = 0; i < Request.Files.Count; i++)
{
   HttpPostedFileBase myFile = Request.Files[i];
}

在MVC中,您始終必須使用html元素的專有名稱才能使用其默認綁定。 在您的情況下,fileupload控件的名稱類似於fileuploadImage1,fileuploadImage2,並且在模型中不存在,因此未綁定。

我建議您命名所有文件上傳元素的名稱。

@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>
}

你要做的動作。

[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();
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM