public IActionResult Create(BannerModel model, IFormFile image) { try { if (ModelState.IsValid) { if (model.ID == 0) { #region For Create var banner = _mapper.Map <Banner>(model); if (image != null) { string urlImage = MyTool.UploadImage(image, "wwwroot", "Image", "Sliders"); if (!string.IsNullOrEmpty(urlImage)) { banner.ImagePath = urlImage; } } db.Banners.Add(banner); db.SaveChanges(); #endregion For Create TempData["StatusMessage"] = "Successfully created"; } else { #region for edit var bannerEdit = _mapper.Map <Banner>(model); if (image != null) { string urlImage = MyTool.UploadImage(image, "wwwroot", "Image", "Sliders"); if (!string.IsNullOrEmpty(urlImage)) { bannerEdit.ImagePath = urlImage; } } db.Update(bannerEdit); db.SaveChanges(); #endregion for edit TempData["StatusMessage"] = "Successfully updated"; } return(RedirectToAction("Index")); } } catch (Exception ex) { TempData["StatusMessage"] = ex.Message; return(View(model)); } return(View(model)); }
public IActionResult ChangeUserInfo(CustomerInfoModel model) { var customerInfor = _mapper.Map <CustomerInfo>(model); var transaction = _context.Database.BeginTransaction(); try { _context.Update(customerInfor); _context.SaveChanges(); transaction.Commit(); } catch (System.Exception) { transaction.Rollback(); } return(RedirectToAction("Index")); }
public IActionResult Create(SettingModel model, IFormFile avata) //setting upload file { try { if (ModelState.IsValid) { if (model.ID == 0) { #region For Create var entity = _mapper.Map <Setting>(model); db.Settings.Add(entity); db.SaveChanges(); #endregion For Create TempData["StatusMessage"] = "Successfully created"; } else { #region for edit var entity = db.Settings.Find(model.ID); entity = _mapper.Map(model, entity); db.Update(entity); db.SaveChanges(); #endregion for edit TempData["StatusMessage"] = "Successfully updated"; } return(RedirectToAction("Index")); } } catch (Exception ex) { TempData["StatusMessage"] = ex.Message; return(View(model)); } return(View(model)); }
public IActionResult Create(WarrantyModel model) { try { if (ModelState.IsValid) { if (model.ID == 0) { #region For Create var warranty = _mapper.Map <CT_WarrantyTime>(model); db.CT_WarrantyTimes.Add(warranty); db.SaveChanges(); #endregion For Create TempData["StatusMessage"] = "Successfully created"; } else { #region for edit var warrantyEdit = _mapper.Map <CT_WarrantyTime>(model); db.Update(warrantyEdit); db.SaveChanges(); #endregion for edit TempData["StatusMessage"] = "Successfully updated"; } return(RedirectToAction("Index")); } } catch (Exception ex) { TempData["StatusMessage"] = ex.Message; return(View(model)); } return(View(model)); }
public IActionResult Create(ProductModel model, IFormFile Avatar, List <IFormFile> DetailImages, string PropertyJsonString) { try { string wwwPath = _env.WebRootPath; string contentPath = _env.ContentRootPath; string AvatarPath = Path.Combine(this._env.WebRootPath, "Image", "Products"); string DetailImagePath = Path.Combine(this._env.WebRootPath, "Image", "Products", "Details"); if (!Directory.Exists(DetailImagePath)) { Directory.CreateDirectory(DetailImagePath); } if (!Directory.Exists(AvatarPath)) { Directory.CreateDirectory(AvatarPath); } List <ProductColorSize> PropertyList = JsonConvert.DeserializeObject <List <ProductColorSize> >(PropertyJsonString); if (ModelState.IsValid) { string fileNameAvata = ""; if (Avatar != null) { fileNameAvata = Path.GetFileName(Avatar.FileName); using (FileStream stream = new FileStream(Path.Combine(AvatarPath, fileNameAvata), FileMode.Create)) { Avatar.CopyTo(stream); } } if (model.ID == 0) { #region For Create var product = _mapper.Map <Product>(model); if (!string.IsNullOrEmpty(fileNameAvata)) { product.ImagePath = fileNameAvata; } db.Products.Add(product); db.SaveChanges(); if (PropertyList != null) { foreach (var item in PropertyList) { item.ProductID = product.ID; item.ProductName = db.Products.Find(product.ID).ProductName; item.NameColor = db.CT_Colors.SingleOrDefault(x => x.ColorId == item.ColorId).Name; db.ProductColorSizes.Add(item); } db.SaveChanges(); } #endregion For Create #region Upload detail image and save to db foreach (var item in DetailImages) { var fileNameTemp = Path.GetFileName(item.FileName); using (FileStream stream = new FileStream(Path.Combine(DetailImagePath, fileNameTemp), FileMode.Create)) { item.CopyTo(stream); } if (!string.IsNullOrEmpty(item.FileName)) { Image DetailImage = new Image(); DetailImage.ImagePath = item.FileName; DetailImage.ReferenceId = product.ID; DetailImage.Type = "Product"; DetailImage.IsShow = true; db.Images.Add(DetailImage); db.SaveChanges(); } } #endregion Upload detail image and save to db TempData["StatusMessage"] = "Successfully created"; } else { #region for edit var product = db.Products.Find(model.ID); product = _mapper.Map(model, product); if (!string.IsNullOrEmpty(fileNameAvata)) { product.ImagePath = fileNameAvata; } db.Update(product); db.SaveChanges(); if (PropertyList != null) { foreach (var item in PropertyList) { item.ProductID = model.ID; item.ProductName = db.Products.Find(model.ID).ProductName; item.NameColor = db.CT_Colors.SingleOrDefault(x => x.ColorId == item.ColorId).Name; if (item.Id == 0) { db.ProductColorSizes.Add(item); } else { db.ProductColorSizes.Update(item); } } db.SaveChanges(); } #endregion for edit #region Upload detail image and save to db foreach (var item in DetailImages) { var fileNameTemp = Path.GetFileName(item.FileName); using (FileStream stream = new FileStream(Path.Combine(DetailImagePath, fileNameTemp), FileMode.Create)) { item.CopyTo(stream); } if (!string.IsNullOrEmpty(item.FileName)) { Image DetailImage = new Image(); DetailImage.ImagePath = item.FileName; DetailImage.ReferenceId = product.ID; DetailImage.Type = "Product"; DetailImage.IsShow = true; db.Images.Add(DetailImage); db.SaveChanges(); } } #endregion Upload detail image and save to db TempData["StatusMessage"] = "Successfully updated"; } return(RedirectToAction("Index")); } } catch (Exception ex) { ViewBag.ColorList = db.CT_Colors.Where(x => x.Active == true).ToList(); #region Load category for dropdownlist List <Category> categoryList = db.Categories.ToList(); model.CategoryList = new List <SelectListItem>(); var temp = new SelectListItem(); temp.Text = "----Assign category of product----"; temp.Value = "0"; model.CategoryList.Add(temp); BindTree(categoryList, null, model.CategoryList); #endregion Load category for dropdownlist TempData["StatusMessage"] = ex.Message; return(View(model)); } return(View(model)); }
public IActionResult Create(CategoryModel model, IFormFile image) { try { if (ModelState.IsValid) { if (model.ID == 0) { #region For Create var category = _mapper.Map <Category>(model); if (image != null) { string urlImage = MyTool.UploadImage(image, "wwwroot", "Image", "Categories"); if (!string.IsNullOrEmpty(urlImage)) { category.Image = urlImage; } } db.Categories.Add(category); db.SaveChanges(); #endregion For Create TempData["StatusMessage"] = "Successfully created"; } else { #region for edit //var categoryEdit = db.Categories.SingleOrDefault(x=> x.ID == model.ID); //Require make a new instane of Category ty var categoryEdit = _mapper.Map <Category>(model); //categoryEdit = _mapper.Map(model, categoryEdit); if (image != null) { string urlImage = MyTool.UploadImage(image, "wwwroot", "Image", "Categories"); if (!string.IsNullOrEmpty(urlImage)) { categoryEdit.Image = urlImage; } } db.Update(categoryEdit); db.SaveChanges(); #endregion for edit TempData["StatusMessage"] = "Successfully updated"; } return(RedirectToAction("Index")); } } catch (Exception ex) { TempData["StatusMessage"] = ex.Message; #region Load category for dropdownlist List <Category> cateList = db.Categories.ToList(); model.LstParentCategory = new List <SelectListItem>(); var tempItem = new SelectListItem(); tempItem.Text = "----Assign parent category role----"; tempItem.Value = "0"; model.LstParentCategory.Add(tempItem); BindTree(cateList, null, model.LstParentCategory); #endregion Load category for dropdownlist return(View(model)); } #region Load category for dropdownlist List <Category> categoryList = db.Categories.ToList(); model.LstParentCategory = new List <SelectListItem>(); var temp = new SelectListItem(); temp.Text = "----Assign parent category role----"; temp.Value = "0"; model.LstParentCategory.Add(temp); BindTree(categoryList, null, model.LstParentCategory); #endregion Load category for dropdownlist return(View(model)); }