public ActionResult Edit(tblProduct tblproduct, FormCollection collection, int?id, List <HttpPostedFileBase> uploadFile) { if (ModelState.IsValid) { if (collection["drMenu"] != "" || collection["drMenu"] != null) { if (id == null) { id = int.Parse(collection["idProduct"]); tblproduct = db.tblProducts.Find(id); } else { tblproduct = db.tblProducts.Find(id); } ViewBag.id = id; int idCate = int.Parse(collection["drMenu"]); tblproduct.idCate = idCate; tblproduct.DateCreate = DateTime.Now; string tag = tblproduct.Tag; string Name = collection["Name"]; string Code = collection["Code"]; string Size = collection["Size"]; string Description = collection["Description"]; string Info = collection["Info"]; string Content = collection["Content"]; string Parameter = collection["Parameter"]; string ImageLinkDetail = collection["ImageLinkDetail"]; string[] listarray = ImageLinkDetail.Split('/'); string imagethum = listarray[listarray.Length - 1]; tblproduct.ImageLinkThumb = "/Images/_thumbs/Images/" + imagethum; string ImageSale = collection["ImageSale"]; if (collection["Price"] != null) { float Price = float.Parse(collection["Price"]); tblproduct.Price = Price; } if (collection["PriceSale"] != null) { float PriceSale = float.Parse(collection["PriceSale"]); tblproduct.PriceSale = PriceSale; } string Warranty = collection["Warranty"]; string Address = collection["Address"]; string Sale = collection["Sale"]; int Ord = 0; if (collection["Ord"] != null) { Ord = int.Parse(collection["Ord"]); tblproduct.Ord = Ord; } bool URL = (collection["URL"] == "false") ? false : true; // bool ProductSale = (collection["ProductSale"] == "false") ? false : true; // bool Vat = (collection["Vat"] == "false") ? false : true; // bool Status = (collection["Status"] == "false") ? false : true; // bool Active = (collection["Active"] == "false") ? false : true; bool New = (collection["New"] == "false") ? false : true; // bool ViewHomes = (collection["ViewHomes"] == "false") ? false : true; // bool Priority = (collection["Priority"] == "false") ? false : true; // string Title = collection["Title"]; string Keyword = collection["Keyword"]; if (tblproduct.Visit != null) { tblproduct.Visit = tblproduct.Visit; } tblproduct.PriceNote = collection["PriceNote"]; tblproduct.Time = collection["Time"]; tblproduct.id = int.Parse(tblproduct.id.ToString()); tblproduct.Name = Name; tblproduct.Code = Code; tblproduct.ImageSale = ImageSale; tblproduct.Description = Description; tblproduct.ProductSale = ProductSale; tblproduct.Content = Content; tblproduct.Info = Info; tblproduct.Size = Size; tblproduct.Parameter = Parameter; tblproduct.ImageLinkDetail = ImageLinkDetail; tblproduct.Vat = Vat; tblproduct.Warranty = Warranty; tblproduct.Sale = Sale; tblproduct.Active = Active; tblproduct.New = New; tblproduct.Priority = Priority; tblproduct.Status = Status; tblproduct.DateCreate = DateTime.Now; tblproduct.ViewHomes = ViewHomes; tblproduct.Title = Title; tblproduct.Keyword = Keyword; string urls = db.tblGroupProducts.Find(idCate).Tag; if (URL == true) { tblproduct.Tag = StringClass.NameToTag(tblproduct.Name); var GroupProduct = db.tblGroupProducts.Find(idCate); clsSitemap.UpdateSitemap("san-pham/" + StringClass.NameToTag(tblproduct.Name), id.ToString(), "Product"); } else { tblproduct.Tag = collection["NameURL"]; var GroupProduct = db.tblGroupProducts.Find(idCate); clsSitemap.UpdateSitemap("san-pham/" + StringClass.NameToTag(tblproduct.Name), id.ToString(), "Product"); } if (Session["Price"] == "1") { var getHtmlWeb = new HtmlWeb(); var listconnectweb = db.tblConnectWebs.Where(p => p.idCate == idCate).ToList(); List <int> Mang = new List <int>(); for (int i = 0; i < listconnectweb.Count; i++) { Mang.Add(int.Parse(listconnectweb[i].idWeb.ToString())); } var listweb = db.tblWebs.Where(p => Mang.Contains(p.id)).ToList(); for (int i = 0; i < listweb.Count; i++) { getHtmlWeb.Load("" + listweb[i].Url + "/Productad/UpdateProduct?Code=" + tblproduct.Code + "&Price=" + tblproduct.Price + "&PriceSale=" + tblproduct.PriceSale + "&Active=" + tblproduct.Active + "&Status=" + tblproduct.Status + ""); } } var Kiemtra = db.tblProducts.Where(p => p.Ord == Ord && p.idCate == idCate && p.id != id).ToList(); if (Kiemtra.Count > 0) { var listProduct = db.tblProducts.Where(p => p.Ord >= Ord && p.idCate == idCate).ToList(); for (int i = 0; i < listProduct.Count; i++) { int idp = int.Parse(listProduct[i].id.ToString()); var productUpdate = db.tblProducts.Find(idp); productUpdate.Ord = productUpdate.Ord + 1; db.SaveChanges(); } } db.SaveChanges(); //thêm tabs var listProductTag = db.tblProductTags.Where(p => p.idp == id).ToList(); for (int i = 0; i < listProductTag.Count; i++) { int ids = listProductTag[i].id; tblProductTag tblproducttag = db.tblProductTags.Find(ids); db.tblProductTags.Remove(tblproducttag); db.SaveChanges(); } string nkeyword = collection["Tabs"];; string[] mangKeyword = nkeyword.Split(','); for (int i = 0; i < mangKeyword.Length; i++) { if (mangKeyword[i] != null && mangKeyword[i] != "") { tblProductTag tblproductTag = new tblProductTag(); tblproductTag.idp = id; tblproductTag.Name = mangKeyword[i]; tblproductTag.Tag = StringClass.NameToTag(mangKeyword[i]); db.tblProductTags.Add(tblproductTag); db.SaveChanges(); } } foreach (string key in Request.Form.Cast <string>().Where(key => key.StartsWith("chek-"))) { var checkbox = ""; checkbox = Request.Form["" + key]; if (checkbox != "false") { Int32 idchk = Convert.ToInt32(key.Remove(0, 5)); tblImageProduct image = db.tblImageProducts.Find(idchk); db.tblImageProducts.Remove(image); db.SaveChanges(); } } TempData["Msg"] = ""; string abc = ""; string def = ""; if (uploadFile != null) { foreach (var item in uploadFile) { if (item != null) { string filename = item.FileName; string path = System.IO.Path.Combine(Server.MapPath("~/Images/ImagesList"), System.IO.Path.GetFileName(item.FileName)); item.SaveAs(path); abc = string.Format("Upload {0} file thành công", uploadFile.Count); def += item.FileName + "; "; tblImageProduct imgp = new tblImageProduct(); imgp.idProduct = id; imgp.Images = "/Images/ImagesList/" + System.IO.Path.GetFileName(item.FileName); db.tblImageProducts.Add(imgp); db.SaveChanges(); } } TempData["Msg"] = abc + "</br>" + def; } //Thêm các thuộc tính var listconnectCre = db.tblConnectCriterias.Where(p => p.idpd == id).ToList(); for (int i = 0; i < listconnectCre.Count; i++) { db.tblConnectCriterias.Remove(listconnectCre[i]); db.SaveChanges(); } foreach (string key in Request.Form.Keys) { var checkbox = ""; if (key.StartsWith("chkCre_")) { checkbox = Request.Form["" + key]; if (checkbox != "false") { int idCri = Convert.ToInt32(key.Remove(0, 7)); tblConnectCriteria tblconnectcre = new tblConnectCriteria(); tblconnectcre.idCre = idCri; tblconnectcre.idpd = id; db.tblConnectCriterias.Add(tblconnectcre); db.SaveChanges(); } } } } if (collection["btnSave"] != null) { Session["Thongbao"] = "<div class=\"alert alert-info alert1\">Bạn đã sửa sản phẩm thành công !<button class=\"close\" data-dismiss=\"alert\">×</button></div>"; return(Redirect("/Productad/Index?idCate=" + int.Parse(collection["drMenu"]))); } if (collection["btnSaveCreate"] != null) { Session["Thongbao"] = "<div class=\"alert alert-info\">Bạn đã thêm thành công, mời bạn thêm sản phẩm mới !<button class=\"close\" data-dismiss=\"alert\">×</button></div>"; return(Redirect("/Productad/Create?id=" + +int.Parse(collection["drMenu"]) + "")); } #region [Updatehistory] Updatehistoty.UpdateHistory("Update Product", Request.Cookies["Username"].Values["Username"].ToString(), Request.Cookies["Username"].Values["UserID"].ToString()); #endregion } return(View(tblproduct)); }
public ActionResult Create(tblProduct tblproduct, FormCollection Collection, string id, List <HttpPostedFileBase> uploadFile) { if ((Request.Cookies["Username"] == null)) { return(RedirectToAction("LoginIndex", "Login")); } string nidCate = Collection["drMenu"]; if (nidCate != "") { tblproduct.idCate = int.Parse(nidCate); int idcate = int.Parse(nidCate); tblproduct.DateCreate = DateTime.Now; tblproduct.Tag = StringClass.NameToTag(tblproduct.Name); tblproduct.DateCreate = DateTime.Now; tblproduct.Visit = 0; string capacity = Collection["Capacity"]; string[] listarray = tblproduct.ImageLinkDetail.Split('/'); string ImageLinkDetail = Collection["ImageLinkDetail"]; string imagethum = listarray[listarray.Length - 1]; tblproduct.ImageLinkThumb = "/Images/_thumbs/Images/" + imagethum; db.tblProducts.Add(tblproduct); db.SaveChanges(); var listprro = db.tblProducts.OrderByDescending(p => p.id).Take(1).ToList(); clsSitemap.CreateSitemap("san-pham/" + tblproduct.Tag, listprro[0].id.ToString(), "Product"); #region [Updatehistory] Updatehistoty.UpdateHistory("Create Product", Request.Cookies["Username"].Values["Username"].ToString(), Request.Cookies["Username"].Values["UserID"].ToString()); #endregion var listproduct = db.tblProducts.OrderByDescending(p => p.id).Take(1).ToList(); int idp = 0; if (listproduct.Count > 0) { idp = int.Parse(listproduct[0].id.ToString()); } TempData["Msg"] = ""; string abc = ""; string def = ""; if (uploadFile != null) { foreach (var item in uploadFile) { if (item != null) { string filename = item.FileName; string path = System.IO.Path.Combine(Server.MapPath("~/Images/ImagesList"), System.IO.Path.GetFileName(item.FileName)); item.SaveAs(path); abc = string.Format("Upload {0} file thành công", uploadFile.Count); def += item.FileName + "; "; tblImageProduct imgp = new tblImageProduct(); imgp.idProduct = idp; imgp.Images = "/Images/ImagesList/" + System.IO.Path.GetFileName(item.FileName); db.tblImageProducts.Add(imgp); db.SaveChanges(); } } TempData["Msg"] = abc + "</br>" + def; } //Thêm thuộc tính foreach (string key in Request.Form.Keys) { var checkbox = ""; if (key.StartsWith("chkCre_")) { checkbox = Request.Form["" + key]; if (checkbox != "false") { int idCri = Convert.ToInt32(key.Remove(0, 7)); tblConnectCriteria tblconnectcre = new tblConnectCriteria(); tblconnectcre.idCre = idCri; tblconnectcre.idpd = idp; db.tblConnectCriterias.Add(tblconnectcre); db.SaveChanges(); } } } //thêm mới tabs string nkeyword = tblproduct.Tabs; string[] mangKeyword = nkeyword.Split(','); for (int i = 0; i < mangKeyword.Length; i++) { if (mangKeyword[i] != null && mangKeyword[i] != "") { tblProductTag tblproductTag = new tblProductTag(); tblproductTag.idp = idp; tblproductTag.Name = mangKeyword[i]; tblproductTag.Tag = StringClass.NameToTag(mangKeyword[i]); db.tblProductTags.Add(tblproductTag); db.SaveChanges(); } } if (Collection["btnSave"] != null) { Session["Thongbao"] = "<div class=\"alert alert-info alert1\">Bạn đã thêm sản phẩm thành công !<button class=\"close\" data-dismiss=\"alert\">×</button></div>"; return(Redirect("/Productad/index?idCate=" + nidCate + "")); } if (Collection["btnSaveCreate"] != null) { Session["Thongbao"] = "<div class=\"alert alert-info\">Bạn đã thêm sản phẩm thành công, mời bạn thêm sản phẩm mới !<button class=\"close\" data-dismiss=\"alert\">×</button></div>"; return(Redirect("/Productad/Create?id=" + nidCate + "")); } } return(View()); }