2012-10-08 49 views
0

错误消息: ObjectStateManager中已存在具有相同键的对象。 ObjectStateManager不能使用同一个键跟踪多个对象。如何在编辑空值的情况下从DB获取值

如果fileupload具有空值(不会更改),我想从数据库获取Image Url。 我的意思是如果我改变smallImage而不改变LargeImage,那么它应该从数据库中获取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); 
    } 

谢谢。

+0

是否使用实体框架? – tomasmcguinness

回答

0

你都加载博客

db.Blogs.Find(blog.ID) 

的副本,并用相同的ID连接另一个上下文

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

这意味着您在上下文中有两份同一博客(不允许)。

我建议更换一个带有代替视图模型贴后背,像

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

感谢您的回复,但没有任何改变。 –

相关问题