2015-12-21 36 views
-2

這裏我想補充的產品和保存圖片的路徑,一切工作正常,圖像路徑保存如何更新映像路徑,並將其保存回數據庫

public ActionResult AddProduct(Product p, HttpPostedFileBase prodImg, decimal[] price) 
        { 
         try 
         { 
          string absoluthFolderPath = Server.MapPath("\\Images"); 
          string pathOfImage = System.IO.Path.GetExtension(prodImg.FileName); 
          string newFileName = Guid.NewGuid() + pathOfImage; 
          absoluthFolderPath += "\\" + newFileName; 
          prodImg.SaveAs(absoluthFolderPath); 

          string relitivePath = @"\Images\" + newFileName; 
          p.ImagePath = relitivePath; 
          p.Blocked = false; 
          new ProductsBL().AddProduct(p); 
          ViewData\["msg"\] = "Successfuly"; 
         } 
         catch(Exception ex) 
         { 

         } 
         ModelState.Clear(); 
         return View(); 
        } 

當試圖更新映像路徑它給我的錯誤顯示在截圖

public ActionResult Update(Product modifieDetails, HttpPostedFileBase updImg) 
      { 
       string absoluthFolderPath = Server.MapPath("\\Images"); 
       string pathOfImage = System.IO.Path.GetExtension(updImg.FileName); 
       string newFileName = Guid.NewGuid() + pathOfImage; 
       absoluthFolderPath += "\\" + newFileName; 
       updImg.SaveAs(absoluthFolderPath); 

       string relitivePath = @"\Images\" + newFileName; 
       modifieDetails.ImagePath = relitivePath; 
       modifieDetails.Blocked = false; 
       new ProductsBL().UpdateProduct(modifieDetails); 
       return RedirectToAction("ListProduct"); 
      } 

[1]: http://i.stack.imgur.com/wgE88.png 
+0

歡迎來到SO!請對我們的問題更具體些。你期望會發生什麼?有沒有任何錯誤信息? – Marijn

+0

這個問題說他想更新它並將它保存回數據庫,這有什麼困惑嗎?國際海事組織,這個問題很好。 – Jasmine

回答

1

您需要拆分這件事:

new ProductsBL().AddProduct(p); 

爲了更新保存到一個實體回到商店,您必須在實體上設置「IsModified」,然後保存上下文。是這樣的...

using (ProductsBL context = new ProductsBL()) { 
    var p = (some query to get it from the store); 
    p.ImagePath = relitivePath; 
    p.Blocked = false; 
    p.IsModified = true; 
    context.SaveChanges(); 
} 

正因爲如此,你要創建一個新的實體,並補充說,到商店,而不是更新現有之一。

而且,如果您使用英文編碼,請修正拼寫:修改,相對,絕對。

相關問題