I have a FileUpload
(from microsoft.web.helpers) that is always null in the Edit
ActionResult
method. In the Create
method, it works fine. Only difference is that its a different model in Edit, where I access the original properties through that. (if that makes sense). Here the code so you can see. First the models and then the controllers:
public class Files
{
public int Id { get; set; }
public List<string> FileName { get; set; }
public Artikel Artikel { get; set; }
}
public class Artikel
{
public int Id { get; set; }
public string Headline { get; set; }
public string Text { get; set; }]
public DateTime Date { get; set; }
public string Author { get; set; }
public bool Proof { get; set; }
public IEnumerable<HttpPostedFileBase> FileUpload { get; set; }
public string TextShort { get { return Text.Substring(0, 50); } }
}
HttpPost create:
[HttpPost]
public ActionResult Create(Artikel artikel)
{
if (ModelState.IsValid)
{
artikel.Date = DateTime.Now;
db.Artikler.Add(artikel);
db.SaveChanges();
foreach (var file in artikel.FileUpload)
{
try
{
if (file.ContentLength > 0)
{
var folder = Server.MapPath("~/uploads/Artikler/" + artikel.Id.ToString());
if (!Directory.Exists(folder))
{
Directory.CreateDirectory(folder);
}
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(folder, fileName);
file.SaveAs(path);
开发者_StackOverflow }
}
catch (Exception ex)
{
//ex something someting..
}
}
return RedirectToAction("Index");
}
return View(artikel);
}
Edit:
public ActionResult Edit(int id)
{
var model = new Files
{
Artikel = db.Artikler.Find(id),
FileName = db.GetPictures(id, Server.MapPath("~/uploads/Artikler/"))
};
return View(model);
}
HttpPost edit:
[HttpPost]
public ActionResult Edit(Files files)
{
if (ModelState.IsValid)
{
db.Entry(files.Artikel).State = EntityState.Modified;
db.SaveChanges();
if (files.Artikel.FileUpload != null)
{
foreach (var file in files.Artikel.FileUpload)
{
if (file.ContentLength > 0)
{
var folder = Server.MapPath("~/uploads/artikler/" + files.Artikel.Id.ToString());
if (!Directory.Exists(folder))
{
Directory.CreateDirectory(folder);
}
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(folder, fileName);
file.SaveAs(path);
}
}
}
return RedirectToAction("Index");
}
return View(files);
}
The markup:
@FileUpload.GetHtml()
So, clearly ive misunderstood something, or done something wrong. Any ideas?
I guess the problem comes from the name
attribute of the <input type="file">
element on the form.
For example in the Create action it looks like this (I suppose because it works):
<input type="file" name="FileUpload" />
and then it is automatically bound to the FileUpload
property of the Artikel
class which is what your Create action takes as argument.
In the Edit action I don't know how it is named. But I suppose the same. But because your Edit action takes Files
as argument it must be named like this:
<input type="file" name="Artikel.FileUpload" />
so that it is bound to the FileUpload
property of the Artikel
property of your Files
model. Also you should make sure that the form containing this file input has the enctype="multipart/form-data"
attribute.
So fix your markup in the view.
精彩评论