2012-10-08 80 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

感謝您的回覆,但沒有任何改變。 –

相關問題