I have an ActionResult which binds data to the model and adds it the the DB. Now what I want, is to have a file uploader, and the ActionResult to store the files and adding their FileName to the DB (so I can show the files/images at a later point). What is the best approach? Here is what I got so far (It can store the files, but im not sure how intelligent EF is, and how complex the datatypes can be):
The model class:
public class Annonce
{
public int Id { get; set; }
public string Company { get; set; }
public string Size { get; set; }
public IEnumerable<HttpPostedFileBase> FileUpload { get; set; }
}
The view (using mircosoft.web.helpers):
@using (Html.BeginForm("Create", "Annonce", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Annonce</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Company)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Company)
@Html.ValidationMessageFor(model => model.Company)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Size)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Size)
@Html.ValidationMessageFor(model => model.Size)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.FileUpload)
</div>
<div class="editor-field">
@FileUpload.GetHtml(uploadText: "Opret")
</div>
</fieldset>
The controller:
[HttpPost]
public ActionResult Create(Annonce annonce)
{
if (ModelState.IsValid)
{
//System.IO stuff
foreach (var file in annonce.FileUpload)
{
if (file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
file.SaveAs(path);
}
}
//Database stuff
db.Annoncer.Add(annonce);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(annonce);
}
This stores the files just fine. Now what I want, is file.FileName
to be stores in the db. At first, I though EF would simply bind the files or filenames from model.FileUpload
to the db. But I guess the datatype is too complex? So I though of making a list<HttpPostedFileBase>
and adding the file.FileName
there? Or maybe create an entire new entity/table where all the filenames are stored as strings with a reference-Id?
Any suggestions?