public ActionResult Edit(EditProductdetails product) { if (ModelState.IsValid) { //Update products var userToUpdateproducts = db.PRODUCTs.SingleOrDefault(u => u.Pid == product.Pid); if (userToUpdateproducts != null) { userToUpdateproducts.PDT_Name = product.Product_Name; //userToUpdateproducts.PDT_CODE = product.Product_Code; userToUpdateproducts.PDT_Price = product.Product_Price; userToUpdateproducts.PDT_Description = product.Product_Description; // userToUpdateproducts.Category_id = product.Category_id; // userToUpdateproducts.Sub_Category_id = Convert.ToInt32(product.Sub_Category_id); // userToUpdateproducts.Brand_id = Convert.ToInt32(product.brand_id); // userToUpdateproducts.offer_value_type_id = product.offer_id; userToUpdateproducts.Mod_user_id =WebSecurity.CurrentUserId; userToUpdateproducts.Mod_Datetime = System.DateTime.Now; // userToUpdateproducts.PDT_offer = product.Product_offer; // userToUpdateproducts.PDT_Discount_price = product.offer_id == 1 ? (product.Product_Price - (product.Product_offer * product.Product_Price) / 100) : product.Product_Price - product.Product_offer; db.SaveChanges(); } //Update PDT_image_Path table var userToUpdatepdt_img_path = db.PDT_IMAGE_PATH.SingleOrDefault(u => u.PDT_ID == product.Pid && u.First_Flag == true); if (userToUpdatepdt_img_path != null) { userToUpdatepdt_img_path.First_Flag = false; //userToUpdatepdt_img_path.Home_Flag = product.Homeflag; userToUpdatepdt_img_path.User_id = WebSecurity.CurrentUserId; userToUpdatepdt_img_path.His_Datetime = System.DateTime.Now; db.SaveChanges(); } var usrHomeFalg = db.PDT_IMAGE_PATH.SingleOrDefault(u => u.PDT_ID == product.Pid && u.Home_Flag == true); if (usrHomeFalg != null) { usrHomeFalg.Home_Flag = product.Homeflag; usrHomeFalg.User_id = WebSecurity.CurrentUserId; usrHomeFalg.His_Datetime = System.DateTime.Now; db.SaveChanges(); } bool Firstflag = false; bool sideflag = false; bool vertiflag = false; bool horiflag = false; if (product.Firstflag == 1) { Firstflag = true; var usrFirstFlag = db.PDT_IMAGE_PATH.SingleOrDefault(u => u.PDT_ID == product.Pid && u.Img_type == "F"); if (usrFirstFlag != null) { usrFirstFlag.First_Flag = Firstflag; usrFirstFlag.User_id = WebSecurity.CurrentUserId; usrFirstFlag.His_Datetime = System.DateTime.Now; db.SaveChanges(); } } if (product.Firstflag == 2) { sideflag = true; var usrsideFlag = db.PDT_IMAGE_PATH.SingleOrDefault(u => u.PDT_ID == product.Pid && u.Img_type == "S"); if (usrsideFlag != null) { usrsideFlag.First_Flag = sideflag; usrsideFlag.User_id = WebSecurity.CurrentUserId; usrsideFlag.His_Datetime = System.DateTime.Now; db.SaveChanges(); } } if (product.Firstflag == 3) { horiflag = true; var usrhoriFlag = db.PDT_IMAGE_PATH.SingleOrDefault(u => u.PDT_ID == product.Pid && u.Img_type == "H"); if (usrhoriFlag != null) { usrhoriFlag.First_Flag = horiflag; usrhoriFlag.User_id = WebSecurity.CurrentUserId; usrhoriFlag.His_Datetime = System.DateTime.Now; db.SaveChanges(); } } if (product.Firstflag == 4) { vertiflag = true; var usrvertiFlag = db.PDT_IMAGE_PATH.SingleOrDefault(u => u.PDT_ID == product.Pid && u.Img_type == "V"); if (usrvertiFlag != null) { usrvertiFlag.First_Flag = vertiflag; usrvertiFlag.User_id = WebSecurity.CurrentUserId; usrvertiFlag.His_Datetime = System.DateTime.Now; db.SaveChanges(); } } //banner Image string ImageName, physicalPath, targetPath; if (product.BannerImagepath != null) { var updBannerImg = db.PDT_IMAGE_PATH.SingleOrDefault(u => u.PDT_ID == product.Pid && u.Banner_Flag == true); if (updBannerImg != null) { ImageName = System.IO.Path.GetFileName(product.BannerImagepath.FileName); physicalPath = Server.MapPath(ConfigurationManager.AppSettings["ORIGINALPATH"] + ImageName); product.BannerImagepath.SaveAs(physicalPath); targetPath = Server.MapPath(ConfigurationManager.AppSettings["RESZEFILEPATH"] + ImageName); product.BannerImagepath.SaveAs(targetPath); updBannerImg.Banner_Flag = true; updBannerImg.Image_Path = ImageName; updBannerImg.User_id = WebSecurity.CurrentUserId; updBannerImg.His_Datetime = System.DateTime.Now; db.SaveChanges(); } else { ImageName = System.IO.Path.GetFileName(product.BannerImagepath.FileName); physicalPath = Server.MapPath(ConfigurationManager.AppSettings["ORIGINALPATH"] + ImageName); product.BannerImagepath.SaveAs(physicalPath); targetPath = Server.MapPath(ConfigurationManager.AppSettings["RESZEFILEPATH"] + ImageName); product.BannerImagepath.SaveAs(targetPath); PDT_IMAGE_PATH objBannerImages = new PDT_IMAGE_PATH(); objBannerImages.img_id = (db.PDT_IMAGE_PATH.Max(i => (int?)i.img_id) ?? 0) + 1; objBannerImages.PDT_ID = product.Pid; objBannerImages.Image_Path = ImageName; objBannerImages.User_id = WebSecurity.CurrentUserId; objBannerImages.Img_type = "B"; objBannerImages.His_Datetime = System.DateTime.Now; objBannerImages.Banner_Flag = true; db.PDT_IMAGE_PATH.Add(objBannerImages); } } //sideImage if (product.sideImagepath != null) { //if exists update the new image var updsideImg = db.PDT_IMAGE_PATH.SingleOrDefault(u => u.PDT_ID == product.Pid && u.Img_type == "S"); if (updsideImg != null) { ImageName = System.IO.Path.GetFileName(product.sideImagepath.FileName); physicalPath = Server.MapPath(ConfigurationManager.AppSettings["ORIGINALPATH"] + ImageName); product.sideImagepath.SaveAs(physicalPath); targetPath = Server.MapPath(ConfigurationManager.AppSettings["RESZEFILEPATH"] + ImageName); product.sideImagepath.SaveAs(targetPath); updsideImg.Image_Path = ImageName; updsideImg.First_Flag = sideflag; updsideImg.User_id = WebSecurity.CurrentUserId; updsideImg.His_Datetime = System.DateTime.Now; db.SaveChanges(); } //if does not exist insert the new image else { ImageName = System.IO.Path.GetFileName(product.sideImagepath.FileName); physicalPath = Server.MapPath(ConfigurationManager.AppSettings["ORIGINALPATH"] + ImageName); product.sideImagepath.SaveAs(physicalPath); targetPath = Server.MapPath(ConfigurationManager.AppSettings["RESZEFILEPATH"] + ImageName); product.sideImagepath.SaveAs(targetPath); PDT_IMAGE_PATH objsideImages = new PDT_IMAGE_PATH(); objsideImages.img_id = (db.PDT_IMAGE_PATH.Max(i => (int?)i.img_id) ?? 0) + 1; objsideImages.PDT_ID = product.Pid; objsideImages.Image_Path = ImageName; objsideImages.First_Flag = sideflag; objsideImages.Img_type = "S"; objsideImages.User_id = WebSecurity.CurrentUserId; objsideImages.His_Datetime = System.DateTime.Now; db.PDT_IMAGE_PATH.Add(objsideImages); } } //verticalImage if (product.verticalImagepath != null) { var updvertiImg = db.PDT_IMAGE_PATH.SingleOrDefault(u => u.PDT_ID == product.Pid && u.Img_type == "V"); if (updvertiImg != null) { ImageName = System.IO.Path.GetFileName(product.verticalImagepath.FileName); physicalPath = Server.MapPath(ConfigurationManager.AppSettings["ORIGINALPATH"] + ImageName); product.verticalImagepath.SaveAs(physicalPath); targetPath = Server.MapPath(ConfigurationManager.AppSettings["RESZEFILEPATH"] + ImageName); product.verticalImagepath.SaveAs(targetPath); updvertiImg.Image_Path = ImageName; updvertiImg.First_Flag = vertiflag; updvertiImg.User_id = WebSecurity.CurrentUserId; updvertiImg.His_Datetime = System.DateTime.Now; db.SaveChanges(); } else { ImageName = System.IO.Path.GetFileName(product.verticalImagepath.FileName); physicalPath = Server.MapPath(ConfigurationManager.AppSettings["ORIGINALPATH"] + ImageName); product.verticalImagepath.SaveAs(physicalPath); targetPath = Server.MapPath(ConfigurationManager.AppSettings["RESZEFILEPATH"] + ImageName); product.verticalImagepath.SaveAs(targetPath); PDT_IMAGE_PATH objvertImages = new PDT_IMAGE_PATH(); objvertImages.img_id = (db.PDT_IMAGE_PATH.Max(i => (int?)i.img_id) ?? 0) + 1; objvertImages.PDT_ID = product.Pid; objvertImages.Image_Path = ImageName; objvertImages.User_id = WebSecurity.CurrentUserId; objvertImages.His_Datetime = System.DateTime.Now; objvertImages.First_Flag = vertiflag; objvertImages.Img_type = "V"; db.PDT_IMAGE_PATH.Add(objvertImages); } } //HorizontalImage if (product.horizontalImagepath != null) { var updhoriImg = db.PDT_IMAGE_PATH.SingleOrDefault(u => u.PDT_ID == product.Pid && u.Img_type == "H"); if (updhoriImg != null) { ImageName = System.IO.Path.GetFileName(product.horizontalImagepath.FileName); physicalPath = Server.MapPath(ConfigurationManager.AppSettings["ORIGINALPATH"] + ImageName); product.horizontalImagepath.SaveAs(physicalPath); targetPath = Server.MapPath(ConfigurationManager.AppSettings["RESZEFILEPATH"] + ImageName); product.horizontalImagepath.SaveAs(targetPath); updhoriImg.Image_Path = ImageName; updhoriImg.First_Flag = horiflag; updhoriImg.User_id = WebSecurity.CurrentUserId; updhoriImg.His_Datetime = System.DateTime.Now; db.SaveChanges(); } else { ImageName = System.IO.Path.GetFileName(product.horizontalImagepath.FileName); physicalPath = Server.MapPath(ConfigurationManager.AppSettings["ORIGINALPATH"] + ImageName); product.horizontalImagepath.SaveAs(physicalPath); targetPath = Server.MapPath(ConfigurationManager.AppSettings["RESZEFILEPATH"] + ImageName); product.horizontalImagepath.SaveAs(targetPath); PDT_IMAGE_PATH objhorzImages = new PDT_IMAGE_PATH(); objhorzImages.img_id = (db.PDT_IMAGE_PATH.Max(i => (int?)i.img_id) ?? 0) + 1; objhorzImages.PDT_ID = product.Pid; objhorzImages.Image_Path = ImageName; objhorzImages.User_id = WebSecurity.CurrentUserId; objhorzImages.His_Datetime = System.DateTime.Now; objhorzImages.First_Flag = horiflag; objhorzImages.Img_type = "H"; db.PDT_IMAGE_PATH.Add(objhorzImages); } } //First Image if (product.Imagepath != null) { var updFirstImg = db.PDT_IMAGE_PATH.SingleOrDefault(u => u.PDT_ID == product.Pid && u.Img_type == "F"); if (updFirstImg != null) { ImageName = System.IO.Path.GetFileName(product.Imagepath.FileName); physicalPath = Server.MapPath(ConfigurationManager.AppSettings["ORIGINALPATH"] + ImageName); product.Imagepath.SaveAs(physicalPath); targetPath = Server.MapPath(ConfigurationManager.AppSettings["RESZEFILEPATH"] + ImageName); product.Imagepath.SaveAs(targetPath); updFirstImg.Image_Path = ImageName; updFirstImg.First_Flag = Firstflag; updFirstImg.User_id = WebSecurity.CurrentUserId; updFirstImg.His_Datetime = System.DateTime.Now; db.SaveChanges(); } } //subscription //Add new Specifications foreach (var p in product.GetType().GetProperties()) { // Console.WriteLine("Property {0} type {1} value {2}", p.Name, p.GetValue(obj, null).GetType().Name, p.GetValue(product, null)); if (db.KEY_Specifications.Any(u => u.spec_col_Name == p.Name)) { var chkdescription = from v in db.Value_Specification join K in db.KEY_Specifications on v.Key_spec_id equals K.spec_col_id where v.Product_id == product.Pid && K.spec_col_Name==p.Name select new { K.spec_col_Name, v.key_Col_Value,v.Spec_value_id }; if (!chkdescription.Any()) { if (p.GetValue(product)!=null) { Value_Specification objvaluespec = new Value_Specification(); objvaluespec.Spec_value_id = (db.Value_Specification.Max(i => (int?)i.Spec_value_id) ?? 0) + 1; objvaluespec.Product_id = product.Pid; objvaluespec.Key_spec_id = db.KEY_Specifications.Where(x => x.spec_col_Name == p.Name).FirstOrDefault().spec_col_id; objvaluespec.key_Col_Value = p.GetValue(product, null).ToString(); objvaluespec.AddUser_id = WebSecurity.CurrentUserId; objvaluespec.mod_user_Id = WebSecurity.CurrentUserId; objvaluespec.Add_Datetime = System.DateTime.Now; objvaluespec.Mod_Datetime = System.DateTime.Now; db.Value_Specification.Add(objvaluespec); db.SaveChanges(); } } } } //Edit old Specifications foreach (var p in product.GetType().GetProperties()) { // Console.WriteLine("Property {0} type {1} value {2}", p.Name, p.GetValue(obj, null).GetType().Name, p.GetValue(product, null)); var subscription = from v in db.Value_Specification join K in db.KEY_Specifications on v.Key_spec_id equals K.spec_col_id where v.Product_id == product.Pid select new { K.spec_col_Name, v.key_Col_Value,v.Spec_value_id }; if (subscription.Any(u => u.spec_col_Name == p.Name)) { foreach (var s in subscription) { if (s.spec_col_Name == p.Name) { var usrspecificationtoupd = db.Value_Specification.SingleOrDefault(u => u.Spec_value_id == s.Spec_value_id); if (usrspecificationtoupd != null) { usrspecificationtoupd.key_Col_Value = p.GetValue(product, null).ToString(); usrspecificationtoupd.mod_user_Id = WebSecurity.CurrentUserId; usrspecificationtoupd.Mod_Datetime = System.DateTime.Now; } } } } } db.SaveChanges(); TempData["Message"] = ConfigurationManager.AppSettings["EDT_SUC"]; return RedirectToAction("Index"); } return View(product); }
public ActionResult Edit(int id = 0) { var model = from p in db.PRODUCTs join m in db.Menus on p.Category_id equals m.Mnu_id join o in db.OFFER_VALUE_TYPE on p.offer_value_type_id equals o.OFR_VAl_Type_Id join im in db.PDT_IMAGE_PATH on p.Pid equals im.PDT_ID from sbtype in db.Menus.Where(e => e.Mnu_id == p.Sub_Category_id).DefaultIfEmpty() from brnd in db.Brands.Where(b => b.BRND_id == p.Brand_id).DefaultIfEmpty() where p.Pid == id && im.First_Flag == true select new { Pid = p.Pid, Product_Code = p.PDT_CODE, Product_Name = p.PDT_Name, Product_Description = p.PDT_Description, Product_Price=p.PDT_Price, Category_id=p.Category_id, Sub_Category_id=p.Sub_Category_id, offer_id=p.offer_value_type_id, brand_id=p.Brand_id, Imagepath=im.Image_Path, Bannerflag=im.Banner_Flag, Firstflag=im.First_Flag, Homeflag=im.Home_Flag, Product_offer=p.PDT_offer, ImageType=im.Img_type }; EditProductdetails editprddetails = null; foreach (var m in model) { editprddetails = new EditProductdetails(); editprddetails.Pid = m.Pid; editprddetails.Product_Code = m.Product_Code; editprddetails.Product_Name = m.Product_Name; editprddetails.Product_Description = m.Product_Description; editprddetails.Product_Price=m.Product_Price; editprddetails.Category_id=m.Category_id; editprddetails.Sub_Category_id=m.Sub_Category_id; editprddetails.offer_id=m.offer_id; //editprddetails.Imagepath=m.Imagepath; editprddetails.brand_id=m.brand_id; //editprddetails.Bannerflag=m.Bannerflag; editprddetails.Firstflag = Convert.ToInt32(m.Firstflag); //editprddetails.Homeflag=m.Homeflag; editprddetails.Product_offer = m.Product_offer; } if (model != null) { var sideimages = (from i in db.PDT_IMAGE_PATH where i.PDT_ID == id select new EditImageItem { ImgId = i.img_id, Imagepath = i.Image_Path, Imagetype = i.Img_type }).ToList(); editprddetails.ImageList = sideimages; } var bannerimages = (from i in db.PDT_IMAGE_PATH where i.PDT_ID == id && i.Banner_Flag==true select new EditImageItem { ImgId = i.img_id, Imagepath = i.Image_Path, Imagetype = i.Img_type }).ToList(); if (bannerimages.Count > 0) { editprddetails.BannerImageName = bannerimages.FirstOrDefault().Imagepath; } if (model != null) { var sideimages = (from i in db.PDT_IMAGE_PATH where i.PDT_ID == id && i.First_Flag==true select new { i.Img_type }).ToList(); foreach (var m in sideimages) { switch (m.Img_type) { case "F": editprddetails.Firstflag = 1; break; case "S": editprddetails.Firstflag = 2; break; case "H": editprddetails.Firstflag = 3; break; case "V": editprddetails.Firstflag = 4; break; default: editprddetails.Firstflag = 1; break; } } } var HomeFalgChk = (from i in db.PDT_IMAGE_PATH where i.PDT_ID == id && i.Home_Flag == true select new { i.Img_type }).ToList(); foreach (var m in HomeFalgChk) { editprddetails.Homeflag = true; } foreach (var p in editprddetails.GetType().GetProperties()) { // Console.WriteLine("Property {0} type {1} value {2}", p.Name, p.GetValue(obj, null).GetType().Name, p.GetValue(product, null)); var subscription = from v in db.Value_Specification join K in db.KEY_Specifications on v.Key_spec_id equals K.spec_col_id where v.Product_id == id select new { K.spec_col_Name, v.key_Col_Value }; if (subscription.Any(u => u.spec_col_Name == p.Name)) { foreach (var s in subscription) { if (s.spec_col_Name == p.Name) { editprddetails.GetType().GetProperty(p.Name).SetValue(editprddetails, s.key_Col_Value, null); } } } } var query = from c in db.Menus where !(from o in db.menuTrees where o.Parent_Menuid != 0 select o.Menu_id) .Contains(c.Mnu_id) select new { c.Mnu_id, c.Mnu_Name }; ViewBag.Menus = new SelectList(query, "mnu_Id", "Mnu_Name", editprddetails.Category_id); ViewBag.Brands = new SelectList(db.Brands.Where(act => act.IsActive == true), "BRND_id", "BRND_Name", editprddetails.brand_id); ViewBag.offers = new SelectList(db.OFFER_VALUE_TYPE, "OFR_VAl_Type_Id", "Ofr_Value_type", editprddetails.offer_id); var subcat = (from c in db.Menus where (from o in db.menuTrees where o.Parent_Menuid != 0 && o.Parent_Menuid == editprddetails.Category_id select o.Menu_id) .Contains(c.Mnu_id) select new { c.Mnu_id, c.Mnu_Name }).ToList(); ViewBag.subcategory = new SelectList(subcat, "mnu_Id", "Mnu_Name", editprddetails.Sub_Category_id); return View(editprddetails); //PRODUCT product = db.PRODUCTs.Find(id); //if (product == null) //{ // return HttpNotFound(); //} //return View(product); }