Also ich möchte meine Plattensammlung Katalog. Ich möchte auch etwas MVC lernen. Also habe ich beschlossen, in MVC eine Katalog-Website zu erstellen. So arbeite ich.Laden Sie mehrere Dateien in MVC3 mit Modellbindung
Ich tauche nur meine Zehen ein, kann aber nicht herausfinden, wie ich mehrere Dateien in meine SQLCE-Datenbank hochlade. Ich bin offen für Optionen hier - speichern Sie die Bilder als BLOBS oder einfach als Dateinamen und laden Sie die Bilder in das Dateisystem hoch.
Mein einfaches Modell ist dies:
public class Record
{
[ScaffoldColumn(false)]
public int RecordId { get; set; }
[Required(ErrorMessage = "Artist is required")]
public string Artist { get; set; }
[Required(ErrorMessage = "Title is required")]
public string Title { get; set; }
[DisplayName("Release Date")]
[DisplayFormat(DataFormatString = "{0:d}")]
public DateTime ReleaseDate { get; set; }
[Required(ErrorMessage = "Format is required")]
public string Format { get; set; }
public string Label { get; set; }
[DisplayName("Catalogue Number")]
public string CatalogueNumber { get; set; }
public string Matrix { get; set; }
public string Country { get; set; }
public IEnumerable<HttpPostedFileBase> Images { get; set; }
public string Notes { get; set; }
}
Meine Ansicht ist:
@model Records.Models.Record
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Record</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Artist)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Artist)
@Html.ValidationMessageFor(model => model.Artist)
</div>
// snipped for brevity
<div class="editor-label">
@Html.LabelFor(model => model.Notes)
</div>
<div class="editor-field">
@Html.TextAreaFor(model => model.Notes)
@Html.ValidationMessageFor(model => model.Notes)
</div>
<div class="editor-field">
<input type="file" name="images" id="image1"/>
<input type="file" name="images" id="image2"/>
<input type="file" name="images" id="image3"/>
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
und meine Create-Methode ist:
[HttpPost]
public ActionResult Create(Record record, IEnumerable<HttpPostedFileBase> images)
{
if (ModelState.IsValid)
{
foreach (HttpPostedFileBase image in images)
{
if (image.ContentLength > 0)
{
var fileName = Path.GetFileName(image.FileName);
var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
image.SaveAs(path);
}
}
db.Records.Add(record);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(record);
}
Allerdings sind Bilder (meine Create-Methode param) immer null und Model.IsValid ist immer falsch.
Warum ist das? Ich habe versucht, meine Bild-Upload-Eingabe als 'Bild', 'Bild', 'Bild [n]' und Bilder ist immer 0.
Ich möchte nicht wirklich ein Plugin für diese verwenden, gibt es eine einfache native MVC-Art? Ich bin offen für Helfer!
Vielen Dank im Voraus.
Haben Sie eine [HttpGet], die zunächst eine Instanz des Modells geht zurück? Ich sehe, du hast den [HttpPost] -Teil. –
@Geovani, ja ich habe eine [HttpGet] -Methode, die nur die Ansicht anzeigt. – Brett
In der HTTPGET geben Sie es eine Instanz des Datensatzes oder nur die normale Rückkehr View(); Können Sie Ihre Frage bearbeiten und den Code für Ihre HTTGET-Methode veröffentlichen? –