mvc model fileupadload 不发送模型数据

mvc model fileupadload does not send model data

我有一个 mvc 5 asp.net 上传图片并为它们创建路径的文件上传。

文件上传成功,但模型数据为空。

这是我的模型:

[Table("Slider")]
public partial class Slider
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public Slider()
    {
        Slider1 = new HashSet<Slider>();
    }

    public int ID { get; set; }

    public string Path { get; set; }

    public int? Slider_ID { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Slider> Slider1 { get; set; }

}

这是控制器部分:

[HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "ID,Path")] Slider slider, List<HttpPostedFileBase> FileContent)
    {
        if (ModelState.IsValid)
        {
            byte[] imageData = null;
            if (Request.Files.Count > 0)
            {
                HttpPostedFileBase poImgFile = Request.Files["Path"];
                using (var binary = new BinaryReader(poImgFile.InputStream))
                {
                    imageData = binary.ReadBytes(poImgFile.ContentLength);
                }
            }
            string picturePath = string.Format(Server.MapPath("~/content/slider/{0}.jpg"), slider.ID);
            CreateDirectory(picturePath);

            using (FileStream writer = new FileStream(picturePath, FileMode.Create))
            {
                writer.Write(imageData, 0, imageData.Length);

            }

            db.Sliders.Add(slider);
            db.SaveChanges();                

            return RedirectToAction("Index");
        }
        return View(slider);
    }

这是视图:

    @using (Html.BeginForm("Create", "Sliders", FormMethod.Post, new { @class = "form-horizontal", role = "form", enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()
    <div class="form-horizontal">
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        <div class="col-12 form-group">
            <div class="row">
                @Html.LabelFor(model => model.Path, "Picture", htmlAttributes: new { @class = "control-label col-12 col-md-2" })
                <div class="col-12 col-md-10">
                    <input type="file" name="Path" id="fileUpload" accept=".png,.jpg,.jpeg,.gif" />                    
                    @Html.ValidationMessageFor(model => model.Path, "", new { @class = "text-danger" })
                </div>

            </div>
        </div>

        <div class="form-group">
            <div class="col-12 text-left">
                <input type="submit" value="create" class="btn btn-success" /> | @Html.ActionLink("back to list", "Index", null, new { @class = "btn btn-primary" })
            </div>
        </div>
    </div>
}

当我检查我的数据库时,我看到 Path 是:

System.Web.HttpPostedFileWrapper

和Slider_ID为空,Slider_ID1也为空。

有什么建议吗?

搜索我以前的代码后,我发现,当在控制器 class 中保存对 db 的更改时,已发送到 Create 方法的 db.SaveChanges(); 滑块参数将获得新 ID。

我在 if:

之后添加了这两行
if (ModelState.IsValid)
            {

                db.Sliders.Add(slider);
                db.SaveChanges();

然后在几行中完成我的其他业务逻辑。