Laden Sie das im MVC-Modell enthaltene Bild hoch

Ich habe folgendes Modell:

public class Photo
{
    public int PhotoId { get; set; }
    public byte[] ImageData { get; set; }
    public DateTime DateUploaded { get; set; }
    public string Description { get; set; }
    public bool IsActive { get; set; }

}

Ich möchte, dass der Benutzer die Details für das Foto eingeben kann und dann das Modell an den Controller sendet. Meine Controller-Aktion ist wie folgt:

[HttpPost]
    public ActionResult Create(WilhanWebsite.DomainClasses.Photo photo)
    {
        if (ModelState.IsValid)
        {
            photo.DateUploaded = DateTime.Now;
            _context.Photos.Add(photo);
            _context.SaveChanges();

            return RedirectToAction("Index");
        }
        //we only get here if there was a problem
        return View(photo);
    }

Meine Ansicht ist wie folgt:

@using (Html.BeginForm()) 
{
@Html.AntiForgeryToken()

<div class="form-horizontal">
    <h4>Photo</h4>
    <hr />
    @Html.ValidationSummary(true)

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

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

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

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

Die Ansicht wird in Ordnung angezeigt und ermöglicht es dem Benutzer, eine Datei von seiner lokalen Festplatte auszuwählen und die anderen Modelldetails einzugeben. Mein Problem ist, dass, obwohl das Modell auf dem Controller in Ordnung ist, die Flags Description, Date und IsActive in Ordnung sind - die Bilddaten sind null.

Kann mir jemand bitte mitteilen, was ich ändern muss, damit das Byte-Array für das Foto in dem Modell enthalten ist, das an den Controller gesendet wird?

Antworten auf die Frage(3)

Ihre Antwort auf die Frage