// GET: Admin/SubCategoryOne/Create public async Task <IActionResult> Create() { //ViewBag.CategoryList = new SelectList(_categoryService.GetAllCategories(), "CategoryId", "CategoryName"); var catlist = await _categoryService.GetAllCategories(); var cvm = new List <CategoryViewModel>(); foreach (var item in catlist) { CategoryViewModel categoryViewModel = new CategoryViewModel() { CategoryId = item.CategoryId, CategoryName = item.CategoryName, Description = item.Description }; cvm.Add(categoryViewModel); } SubCategoryOneViewModel viewModel = new SubCategoryOneViewModel() { MyCategoryList = cvm }; return(View(viewModel)); }
// GET: Admin/SubCategoryOne/Edit/5 public async Task <IActionResult> Edit(int?id) { if (id == null) { return(NotFound()); } var subCategoryOne = await _context.SubCategoryOne.FindAsync(id); SubCategoryOneViewModel scViewModel = PrepareSubCategoryOneViewModelFromSubCategoryOneEntity(subCategoryOne); var catlist = await _categoryService.GetAllCategories(); var cvm = new List <CategoryViewModel>(); foreach (var item in catlist) { CategoryViewModel categoryViewModel = new CategoryViewModel() { CategoryId = item.CategoryId, CategoryName = item.CategoryName, Description = item.Description }; cvm.Add(categoryViewModel); } if (scViewModel == null) { return(NotFound()); } scViewModel.MyCategoryList = cvm; return(View(scViewModel)); }
public async Task <IActionResult> Create(SubCategoryOneViewModel subCategoryVM, IFormFile file, IFormCollection fc) { if (ModelState.IsValid) { try { subCategoryVM.CategoryId = Convert.ToInt32(fc["CategoryId"]); SubCategoryOne subCategoryOne = PrepareSubCategoryOneFromViewModel(subCategoryVM); _context.Add(subCategoryOne); var result = await _context.SaveChangesAsync(); if (result > 0) { string webRootPath = _hostingEnvironment.WebRootPath; var files = HttpContext.Request.Form.Files; //var productImage = new ProductImage(); if (files[0] != null && files[0].Length > 0) { //when user uploads an image var uploads = Path.Combine(webRootPath, "Images"); string uploadedImageName = files[0].FileName.Substring(0, files[0].FileName.LastIndexOf(".")); var extension = files[0].FileName.Substring(files[0].FileName.LastIndexOf("."), files[0].FileName.Length - files[0].FileName.LastIndexOf(".")); using (var filestream = new FileStream(Path.Combine(uploads, uploadedImageName + subCategoryOne.SubCategoryOneId + extension), FileMode.Create)) { files[0].CopyTo(filestream); } subCategoryOne.SC_ImagePath = @"\Images\" + uploadedImageName + subCategoryOne.SubCategoryOneId + extension; subCategoryOne.SC_ImageName = uploadedImageName; } else { //when user does not upload image var uploads = Path.Combine(webRootPath, @"Images\" + SD.DefaultSubCategoryImage); System.IO.File.Copy(uploads, webRootPath + @"\Images\" + subCategoryOne.SubCategoryName + subCategoryOne.CategoryId + ".PNG"); subCategoryOne.SC_ImagePath = @"\Images\" + subCategoryOne.CategoryId + ".PNG"; subCategoryOne.SC_ImageName = subCategoryOne.SubCategoryName; } var finalResult = await _context.SaveChangesAsync(); _logger.LogInformation("SubCategory created successfully"); } return(RedirectToAction(nameof(Index))); } catch (Exception ex) { _logger.LogError(ex.Message, new[] { "Create", "SubCategoryController" }); } } return(View(subCategoryVM)); }
// GET: Admin/SubCategoryOne/Details/5 public async Task <IActionResult> Details(int?id) { if (id == null) { return(NotFound()); } var subCategoryOne = await _context.SubCategoryOne .FirstOrDefaultAsync(m => m.SubCategoryOneId == id); if (subCategoryOne == null) { return(NotFound()); } SubCategoryOneViewModel viewModel = PrepareSubCategoryOneViewModelFromSubCategoryOneEntity(subCategoryOne); return(View(viewModel)); }
/// <summary> /// Prepare SubCategoryOne From SubCategoryViewModel /// </summary> /// <param name="subCategoryVM"></param> /// <returns></returns> private SubCategoryOne PrepareSubCategoryOneFromViewModel(SubCategoryOneViewModel subCategoryVM) { SubCategoryOne subCategoryOne = new SubCategoryOne(); if (subCategoryVM != null) { subCategoryOne.CategoryId = subCategoryVM.CategoryId; subCategoryOne.SubCategoryName = subCategoryVM.SubCategoryName; subCategoryOne.Description = subCategoryVM.Description; subCategoryOne.DisplayOrder = subCategoryVM.DisplayOrder; subCategoryOne.IsActive = subCategoryVM.IsActive; subCategoryOne.CreatedDate = subCategoryVM.CreatedDate; subCategoryOne.UpdatedDate = subCategoryVM.UpdatedDate; subCategoryOne.SC_ImagePath = subCategoryVM.ImagePath; //Temporarly storing b'coz this column is expecting not null.so later we will update exact name. subCategoryOne.SC_ImageName = "Default"; } return(subCategoryOne); }