为什么HttpPostedFileBase执行表单提交后返回空值?

时间:2018-07-13 03:07:17

标签: c# asp.net-mvc-5

我一直在尝试使用Entity Framework从MVC5中的表单上载两个图像文件。当我用POST向表单提交时,HttpPostedFileBase返回空值。

查看代码:

<div class="form-group">
    @Html.LabelFor(model => model.Fevicon_Icon, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
            @Html.TextBoxFor(model => model.Fevicon_Icon, new { type = "file", @Name = "Fevicon_Icon" })
            @Html.ValidationMessageFor(model => model.Fevicon_Icon, "", new { @class = "text-danger" })
    </div>
</div>

<div class="form-group">
    @Html.LabelFor(model => model.Project_Logo, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.TextBoxFor(model => model.Project_Logo, new { type = "file", @Name = "Project_Logo" })
        @Html.ValidationMessageFor(model => model.Project_Logo, "", new { @class = "text-danger" })
    </div>
</div>

控制器代码:

[HttpPost]
public ActionResult UpdateProjectInfo(Project_Info PInfo, HttpPostedFileBase Fevicon_Icon, HttpPostedFileBase Project_Logo)
{
    if (Session["UserName"] == null)
    {
        return RedirectToAction("UserLogin", "Dashboard");
    }
    else
    {
        using (FLYHIGHCMSEntities dbCon = new FLYHIGHCMSEntities())
        {
            Int32 ProjID = (int)TempData["p_id"];
            var ProjInfo = dbCon.Project_Info.Where(x => x.p_id == ProjID).FirstOrDefault();
            if (ProjInfo != null)
            {
                ProjInfo.Project_Name = PInfo.Project_Name;
                ProjInfo.Project_Address = PInfo.Project_Address;
                ProjInfo.Contact_Details = PInfo.Contact_Details;
                ProjInfo.Establish_Date = PInfo.Establish_Date;
                ProjInfo.TypeOfProject = PInfo.TypeOfProject;
                if (Fevicon_Icon != null)
                {
                    string ImageName = System.IO.Path.GetFileName(Fevicon_Icon.FileName);
                    string physicalPath = Server.MapPath("~/Images/" + ImageName);
                    Fevicon_Icon.SaveAs(physicalPath);
                    ProjInfo.Fevicon_Icon = ImageName;

                }
                if (Project_Logo != null)
                {
                    string ImageName = System.IO.Path.GetFileName(Project_Logo.FileName);
                    string physicalPath = Server.MapPath("~/Images/" + ImageName);
                    Project_Logo.SaveAs(physicalPath);
                    ProjInfo.Project_Logo = ImageName;
                }
                dbCon.Entry(ProjInfo).State = EntityState.Modified;
                dbCon.SaveChanges();
                ViewBag.UpdateMessage = "Detail updated successfully.";
            }
            return RedirectToAction("Index");
        }
    }
}

0 个答案:

没有答案