如何在使用Null值编辑条目时从DB获取值

时间:2012-10-08 07:52:22

标签: asp.net-mvc asp.net-mvc-3 entity-framework

错误消息: 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);
    }

谢谢。

2 个答案:

答案 0 :(得分: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;
            }

答案 1 :(得分:0)

您正在加载博客的副本

 db.Blogs.Find(blog.ID)

并将具有相同ID的另一个附加到上下文

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

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

我建议用viewmodel替换回来的那个,比如

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");
}