MVC3 FileUpload(Web 帮助程序)在 Edit 方法中始终为 null
我有一个 FileUpload
(来自 microsoft.web.helpers),它在 Edit
ActionResult
方法中始终为 null。在Create
方法中,它工作得很好。唯一的区别是它在编辑中的模型不同,我通过它访问原始属性。 (如果这是有道理的话)。这里有代码,你可以看看。首先是模型,然后是控制器:
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 创建:
[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);
}
}
catch (Exception ex)
{
//ex something someting..
}
}
return RedirectToAction("Index");
}
return View(artikel);
}
编辑:
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 编辑:
[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);
}
标记:
@FileUpload.GetHtml()
所以,显然我误解了某些东西,或者做错了一些事情。有什么想法吗?
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);
}
}
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我猜问题来自于表单上
元素的
name
属性。例如,在 Create 操作中,它看起来像这样(我想是因为它有效):
然后它会自动绑定到
Artikel
类的FileUpload
属性,这就是您的创建操作作为参数。在编辑操作中我不知道它是如何命名的。但我想也是一样。但是,由于您的“编辑”操作采用
Files
作为参数,因此必须按如下方式命名:以便将其绑定到
Artikel
属性的FileUpload
属性您的Files
模型。此外,您还应该确保包含此文件输入的表单具有enctype="multipart/form-data"
属性。因此,请修复视图中的标记。
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):
and then it is automatically bound to the
FileUpload
property of theArtikel
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:so that it is bound to the
FileUpload
property of theArtikel
property of yourFiles
model. Also you should make sure that the form containing this file input has theenctype="multipart/form-data"
attribute.So fix your markup in the view.