Bild hochladen enthalten im MVC-Modell

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

}

Möchte ich dem Benutzer die Möglichkeit zu geben Sie die details für die Foto-dann poste das Modell der Steuerung. 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>
}

Ansicht wird angezeigt ok und erlaubt dem Benutzer, wählen Sie eine Datei von Ihrem lokalen Datenträger aus und tragen Sie die weiteren details der Modelle.
Mein problem ist, dass, obwohl das Modell gepostet ist der controller ok, die Beschreibung, das Datum und die IsActive-flags aufgefüllt werden ok - die Bild-Daten null ist.

Könnte jemand bitte lassen Sie mich wissen, was ich ändern muss, so dass das byte-array für das Foto wird in das Modell aufgenommen wurden geschrieben, um den controller?

InformationsquelleAutor der Frage Rob Bowman | 2013-12-07

Schreibe einen Kommentar