Upload multiple files to model-bound MVC3

So, I want to catalog my collection of records. I also want to learn some MVC. So I decided to create a record cataloging site in MVC. This is how I work.

I just dip my socks, but I can't figure out how to upload multiple files to the SQLCE database. I am open for settings here - store images as BLOBS or just as file names and upload images to the file system.

My simple model is this:

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

My view:

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

and my Create method:

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

However, the images (my Create method param) are always null, and Model.IsValid is always false.

Why is this? I tried calling my input for loading the image as "image", "image", "image [n]", and the images are always 0.

- , MVC? !

.

+3
2

. .

, multipart/form-data (@using (Html.BeginForm()))

@using (Html.BeginForm()) @using (Html.BeginForm("action", "controller", FormMethod.Post, new { enctype = "multipart/form-data" }))

, .

, ~ Will

: , , , , , ?

+2

A Perfect blog

+2

All Articles