0

I have a mvc 5 asp.net file upload that upload pictures and create path for them.

File uploaded successfully, but model data does comes in null.

This is my model:

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

}

This is Controller part:

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

And this is the view:

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

When I check my database I see that Path is :

System.Web.HttpPostedFileWrapper

and Slider_ID is null and Slider_ID1 is null too.

Any suggestions?

Reza Akraminejad
  • 1,412
  • 3
  • 24
  • 38
  • I'm not sure this will solve it fully, but you are not passing the Id back from the view when submitting. Try adding @Html.HiddenFor(model => model.ID) right below @Html.AntiForgeryToken() – Lasse Holm Aug 02 '19 at 17:37
  • @LasseHolm It didn't solve my problem. Receive null again – Reza Akraminejad Aug 03 '19 at 06:27

1 Answers1

0

After searching my previous codes, I've found that, when saving changes to db in controller class, db.SaveChanges(); slider argument which had been sent to Create method, will get new ID.

I've added these 2 lines after if:

if (ModelState.IsValid)
            {

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

and done my other business logic in lines after that.

Reza Akraminejad
  • 1,412
  • 3
  • 24
  • 38