Пример #1
0
 public ActionResult EditProduct(SanPham sp, HttpPostedFileBase linkanh)
 {
     ViewBag.danhmucsanpham = dmDao.li();
     ViewBag.loaisanpham    = loaiSPDao.dsLoaiSP();
     try
     {
         // lấy tên của hình ảnh
         var tenfile = Path.GetFileName(linkanh.FileName);
         // tạo đường dẫn
         var duongdan = Path.Combine(Server.MapPath("/Photo"), tenfile);
         if (System.IO.File.Exists(duongdan))
         {
             sp.linkanh  = linkanh.FileName;
             ViewBag.loi = "Hình ảnh đã tồn tại";
         }
         else
         {
             linkanh.SaveAs(duongdan);
             sp.linkanh = linkanh.FileName;
         }
     }
     catch (Exception)
     {
         SanPhamDao spDao = new SanPhamDao();
         sp.linkanh = spDao.searchProduct(sp.masanpham).linkanh;
     }
     if (ModelState.IsValid)
     {
         SanPhamDao spDao = new SanPhamDao();
         spDao.Edit(sp);
         return(Redirect("Index"));
     }
     else
     {
         return(View("Edit", sp));
     }
 }