public IActionResult Edit(int id) { ViewBag.AllMainCategory = _unitofWork.mainCategory.GetAll().Where(x => x.isdeleted == false).Select(x => new SelectListItem() { Text = x.name, Value = x.id.ToString() }); var objcategory = _unitofWork.productMaster.Get(id); if (objcategory == null) { return(NotFound()); } var model = new productMasterViewModel() { id = objcategory.id, mainCategroyId = _unitofWork.subcategory.Get(objcategory.subCategroyId).mainCategroyId, subCategroyId = objcategory.subCategroyId, name = objcategory.name, imgname = objcategory.img, description = objcategory.description }; ViewBag.AllSubcategory = _unitofWork.subcategory.GetAll().Where(x => x.isdeleted == false && x.mainCategroyId == model.mainCategroyId).Select(x => new SelectListItem() { Text = x.name, Value = x.id.ToString() }); return(View(model)); }
public IActionResult Create() { ViewBag.AllMainCategory = _unitofWork.mainCategory.GetAll().Where(x => x.isdeleted == false).Select(x => new SelectListItem() { Text = x.name, Value = x.id.ToString() }); var model = new productMasterViewModel(); return(View(model)); }
public async Task <IActionResult> Create(productMasterViewModel model) { if (ModelState.IsValid) { // id, subCategroyId, name, img, description, isdeleted, isactive var objcategory = new ProductMaster { id = model.id , name = model.name , subCategroyId = model.subCategroyId , description = model.description , isdeleted = false , isactive = false }; if (model.img != null && model.img.Length > 0) { var uploadDir = @"uploads/ProductMaster"; var fileName = Path.GetFileNameWithoutExtension(model.img.FileName); var extesion = Path.GetExtension(model.img.FileName); var webRootPath = _hostingEnvironment.WebRootPath; fileName = DateTime.UtcNow.ToString("yymmssfff") + fileName + extesion; var path = Path.Combine(webRootPath, uploadDir, fileName); FileStream fs = new FileStream(path, FileMode.Create); await model.img.CopyToAsync(fs); fs.Close(); objcategory.img = '/' + uploadDir + '/' + fileName; } _unitofWork.productMaster.Add(objcategory); bool res = _unitofWork.Save(); TempData["success"] = "Record Save successfully"; return(RedirectToAction(nameof(Index))); } else { ViewBag.AllMainCategory = _unitofWork.mainCategory.GetAll().Where(x => x.isdeleted == false).Select(x => new SelectListItem() { Text = x.name, Value = x.id.ToString() }); return(View(model)); } return(View()); }
public async Task <IActionResult> Edit(productMasterViewModel model) { if (ModelState.IsValid) { var storeobj = _unitofWork.productMaster.Get(model.id); if (storeobj == null) { TempData["error"] = "Record Not Found"; return(NotFound()); } storeobj.id = model.id; storeobj.subCategroyId = model.subCategroyId; storeobj.name = model.name; storeobj.description = model.description; if (model.img != null && model.img.Length > 0) { var uploadDir = @"uploads/ProductMaster"; var fileName = Path.GetFileNameWithoutExtension(model.img.FileName); var extesion = Path.GetExtension(model.img.FileName); var webRootPath = _hostingEnvironment.WebRootPath; if (storeobj.img != null) { var imagePath = webRootPath + storeobj.img.ToString().Replace("/", "\\"); if (System.IO.File.Exists(imagePath)) { System.IO.File.Delete(imagePath); } } fileName = DateTime.UtcNow.ToString("yymmssfff") + fileName + extesion; var path = Path.Combine(webRootPath, uploadDir, fileName); FileStream fs = new FileStream(path, FileMode.Create); await model.img.CopyToAsync(fs); fs.Close(); storeobj.img = '/' + uploadDir + '/' + fileName; } _unitofWork.productMaster.Update(storeobj); bool res = _unitofWork.Save(); TempData["success"] = "Record Update successfully"; return(RedirectToAction(nameof(Index))); } else { ViewBag.AllMainCategory = _unitofWork.mainCategory.GetAll().Where(x => x.isdeleted == false).Select(x => new SelectListItem() { Text = x.name, Value = x.id.ToString() }); var objcategory = _unitofWork.productMaster.Get(model.id); if (objcategory == null) { return(NotFound()); } var model1 = new productMasterViewModel() { id = objcategory.id, mainCategroyId = _unitofWork.subcategory.Get(objcategory.subCategroyId).mainCategroyId, subCategroyId = objcategory.subCategroyId, name = objcategory.name, imgname = objcategory.img, description = objcategory.description }; ViewBag.AllSubcategory = _unitofWork.subcategory.GetAll().Where(x => x.isdeleted == false && x.mainCategroyId == model.mainCategroyId).Select(x => new SelectListItem() { Text = x.name, Value = x.id.ToString() }); return(View(model1)); } }