0

错误消息: ObjectStateManager 中已存在具有相同键的对象。ObjectStateManager 无法跟踪具有相同键的多个对象。

如果 fileupload 具有空值(不更改),我想从数据库中获取 Image Url。我的意思是如果我改变 smallImage 而不是改变 LargeImage,那么它应该从 DB 中获取 largeImage 值。

[HttpPost]
    public ActionResult Edit(Blog blog, HttpPostedFileBase smallImage, HttpPostedFileBase largeImage)
    {
        if (ModelState.IsValid)
        {
            if (smallImage != null)
            {
                blog.SmallImage = smallImage.ContentLength + "_" + smallImage.FileName;
                string filepath = Path.Combine(Server.MapPath("~/Content/Blog/"), smallImage.ContentLength + "_" + smallImage.FileName);
                smallImage.SaveAs(filepath);
            }
            else
            {
                blog.SmallImage = db.Blogs.Find(blog.ID).SmallImage;
            }
            if (largeImage != null)
            {
                blog.LargeImage = largeImage.ContentLength + "_" + largeImage.FileName;
                string filepath = Path.Combine(Server.MapPath("~/Content/Blog/"), largeImage.ContentLength + "_" + largeImage.FileName);
                largeImage.SaveAs(filepath);
            }
            else
            {
                blog.LargeImage = db.Blogs.Find(blog.ID).LargeImage;
            }
            blog.PostDate = Convert.ToDateTime(DateTime.Now.ToShortDateString());
            db.Entry(blog).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(blog);
    }

谢谢你。

4

2 回答 2

0

看起来这里的问题是您两次加载同一个博客。

而是加载一次,如下所示:

Blog existingBlog = db.Blogs.Find(blog.ID);
if (smallImage != null)
            {
                blog.SmallImage = smallImage.ContentLength + 
                                        "_" + smallImage.FileName;
                string filepath = Path.Combine(Server.MapPath("~/Content/Blog/"),
                           smallImage.ContentLength + "_" + smallImage.FileName);
                smallImage.SaveAs(filepath);
            }
            else
            {
                blog.SmallImage = existingBlog.SmallImage;
            }
            if (largeImage != null)
            {
                blog.LargeImage = largeImage.ContentLength + "_" +
                                     largeImage.FileName;
                string filepath = Path.Combine(Server.MapPath("~/Content/Blog/"),
                                                 largeImage.ContentLength + "_" +
                                                 largeImage.FileName);
                largeImage.SaveAs(filepath);
            }
            else
            {
                blog.LargeImage = existingBlog.LargeImage;
            }
于 2012-10-08T07:56:32.513 回答
0

你们都在加载博客的副本

 db.Blogs.Find(blog.ID)

并将另一个具有相同 id 的内容附加到上下文中

db.Entry(blog).State = EntityState.Modified;

意味着您在上下文中有 2 个相同博客的副本(不允许)。

我建议用视图模型替换回发的那个,比如

public ActionResult Edit(BlogViewModel viewModel, HttpPostedFileBase smallImage, HttpPostedFileBase largeImage)
{
    if (!ModelState.IsValid)
    {
        return View(viewModel);
    }
        var blog =  db.Blogs.Find(viewModel.ID);
        if (smallImage != null)
        {
            blog.SmallImage = smallImage.ContentLength + "_" + smallImage.FileName;
            string filepath = Path.Combine(Server.MapPath("~/Content/Blog/"), smallImage.ContentLength + "_" + smallImage.FileName);
            smallImage.SaveAs(filepath);
        }

        if (largeImage != null)
        {
            blog.LargeImage = largeImage.ContentLength + "_" + largeImage.FileName;
            string filepath = Path.Combine(Server.MapPath("~/Content/Blog/"), largeImage.ContentLength + "_" + largeImage.FileName);
            largeImage.SaveAs(filepath);
        }


        blog.Title = viewModel.Title;
        blog.Body = viewModel.Body; //etc

        db.SaveChanges();
        return RedirectToAction("Index");
}
于 2012-10-08T08:03:15.727 回答