多个图像上传循环只添加一个图像ID和路径数据库

时间:2016-11-27 19:24:15

标签: c# asp.net-mvc asp.net-mvc-4

您好我正在尝试创建一个mvc应用程序,允许用户将多张照片上传到文件夹,并将Id和路径保存在数据库中。目前,我的应用程序将所有文件添加到文件夹中,但只将最后选择的图像ID和路径添加到数据库中。

感谢您对此问题的任何帮助

视图

 @using (Html.BeginForm("Create", "Home", null, FormMethod.Post,
                                                              new { enctype = "multipart/form-data" }))
        {
            @Html.AntiForgeryToken()
            @Html.ValidationSummary(true)
            <fieldset>

                <legend>Image</legend>
                <form action="" method="post" enctype="multipart/form-data">

                    <div class="editor-label">
                        @Html.LabelFor(model => model.ImagePath)

                    </div>
                    <div class="editor-field">
                        <input id="ImagePath" title="Upload a product image" multiple="multiple" type="file" name="files" />

                        @*<img class="img-thumbnail" width="150" height="150" src="@Url.Action("GetImage", "Home",new { Model.ID })" />*@
                    </div>
                    <p><input type="submit" value="Create" /></p>
                </form>
            </fieldset>
        }
        <div>

控制器

public ViewResult Create()
            {
                return View("Create", new Image());

            }

        [HttpPost]
        public ActionResult Create(Image Image, IEnumerable<HttpPostedFileBase> files)
        {

         if (ModelState.IsValid)
            {


            foreach (var file in files)
            {
                if (file.ContentLength > 0)
                {

                    file.SaveAs(HttpContext.Server.MapPath("~/Img/") + file.FileName);
                    Image.ImagePath = file.FileName;

                }
                repository.Create(Image);
            }
           }
           return RedirectToAction("Index");

        }

模型

public partial class Image
{
    [Key]
    public int ID { get; set; }
    public string ImagePath { get; set; }
}

EFRepositoty

public void  Create(Image Image)
            {


                if (Image.ID == 0)
                {
                    context.Images.Add(Image);
                }
                else
                {
                    Image dbEntry = context.Images.Find(Image.ID);
                    if (dbEntry != null)
                    {
                        dbEntry.ImagePath = Image.ImagePath;

                    }
                }
                context.SaveChanges();

        }

2 个答案:

答案 0 :(得分:0)

这是因为您正在重定向到循环内的索引,因此它只处理第一个文件。在循环外部移动ModelState.IsValid检查

if(!ModelState.IsValud)
    return View();

然后将重定向放到方法的最后一行。

Image实体发生的事情是,第一次为对象提供ID时保存,然后为循环中的每个其他文件保存,它只是更新数据库中的相同记录,这就是为什么你只能看到最后一个文件在数据库中。

在每个循环结束时,你应该设置image = null将解决问题。

答案 1 :(得分:0)

我使用的解决方案基于Stephens评论,尽管Matts是一个同样好的解决方案:

                    Image image = new Image();
                    if (file.ContentLength > 0)
                    {
                        file.SaveAs(HttpContext.Server.MapPath("~/Img/") + file.FileName);
                        image.ImagePath = file.FileName;
                    }
                    repository.Create(image);
相关问题