问题
So I want to catalogue my record collection. I also want to learn some MVC. So I decided to build a record cataloguing website in MVC. That's how I work.
I'm just dipping my toes in but cannot figure out how to upload multiple files to my SQLCE database. I'm open to options here - store the images as BLOBS or simply as filenames and upload the images to the filesystem.
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 is:
@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 is:
[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, images (my Create method param) is always null and Model.IsValid is always false.
Why is this? I've tried naming my image upload input as 'image', 'Image', 'image[n]' and images is always 0.
I don't really want to use any plugin for this, is there a simple native MVC way? I'm open to helpers!
Thanks in advance.
回答1:
I've had this before. This might be the fix.
Looks like you are missing the multipart/form-data
in your view (@using (Html.BeginForm())
)
Try replacing @using (Html.BeginForm())
with
@using (Html.BeginForm("action", "controller", FormMethod.Post, new { enctype = "multipart/form-data" }))
Obviously replace action and controller with yours.
Hope this helps ~ Will
Edit: Sorry, just seen the date posted, if you havn't figured it out then maybe this was it?
回答2:
A Perfect blog About this Here
来源:https://stackoverflow.com/questions/10127648/upload-multiple-files-in-mvc3-with-model-binding