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



嗨,我试图创建一个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();
        }

这是因为您正在将循环内部索引重定向,因此它仅处理第一个文件。将您的modelState.ISVALID检查在循环

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

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

图像实体发生了什么是在第一次给对象给对象的ID然后在循环中彼此的文件时,它只是在数据库中更新相同的记录,这就是为什么您只会看到最后一个文件在db。

在每个循环的末端您应该设置image = null将修复事物。

我使用的解决方案是基于斯蒂芬斯的评论,尽管马特同样好的解决方案:

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

最新更新