2015-12-21 32 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(); 
} 

正因为如此,你要创建一个新的实体,并补充说,到商店,而不是更新现有之一。

而且,如果您使用英文编码,请修正拼写:修改,相对,绝对。

相关问题