MVC3 FileUpload(Web 帮助程序)在 Edit 方法中始终为 null

发布于 2024-12-03 22:12:00 字数 3512 浏览 0 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泅人 2024-12-10 22:12:00

我猜问题来自于表单上 元素的 name 属性。

例如,在 Create 操作中,它看起来像这样(我想是因为它有效):

<input type="file" name="FileUpload" />

然后它会自动绑定到 Artikel 类的 FileUpload 属性,这就是您的创建操作作为参数。

在编辑操作中我不知道它是如何命名的。但我想也是一样。但是,由于您的“编辑”操作采用 Files 作为参数,因此必须按如下方式命名:

<input type="file" name="Artikel.FileUpload" />

以便将其绑定到 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):

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文