public async Task <IActionResult> Create(ItemCatagoryViewModel itemCatagoryViewModel) { var itmeCatagory = new ItemCatagory(); itmeCatagory.Id = itemCatagoryViewModel.Id; itmeCatagory.Name = itemCatagoryViewModel.Name; itmeCatagory.Status = 0; string photo1 = "No File"; if (itemCatagoryViewModel.Photo != null) { string uniqueFileName = null; string stringCutted = itemCatagoryViewModel.Photo.FileName.Split('.').Last(); uniqueFileName = Guid.NewGuid().ToString() + "." + stringCutted; photo1 = uniqueFileName; InputFile fileUpload = new InputFile(_hostingEnvironment); fileUpload.Uploadfile("files/item_catagories", itemCatagoryViewModel.Photo, photo1); } itmeCatagory.PhotoName1 = photo1; _context.Add(itmeCatagory); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); }
// GET: ItemCatagories/Edit/5 public async Task <IActionResult> Edit(int?id) { if (id == null) { return(NotFound()); } var itemCatagory = await _context.ItemCatagories.FindAsync(id); var itemCatagoryViewModel = new ItemCatagoryViewModel(); itemCatagoryViewModel.Id = itemCatagory.Id; itemCatagoryViewModel.Name = itemCatagory.Name; itemCatagoryViewModel.OldPhotoName = itemCatagory.PhotoName1; itemCatagoryViewModel.Status = itemCatagory.Status; if (itemCatagory == null) { return(NotFound()); } return(View(itemCatagoryViewModel)); }
public async Task <IActionResult> Edit(int id, ItemCatagoryViewModel itemCatagoryViewModel) { if (id != itemCatagoryViewModel.Id) { return(NotFound()); } var itemCatagory = new ItemCatagory(); itemCatagory.Id = itemCatagoryViewModel.Id; itemCatagory.Name = itemCatagoryViewModel.Name; itemCatagory.Status = 0; InputFile fileUpload = new InputFile(_hostingEnvironment); itemCatagory.PhotoName1 = itemCatagoryViewModel.OldPhotoName; if (itemCatagoryViewModel.Photo != null) { string uniqueFileName = null; string stringCutted = itemCatagoryViewModel.Photo.FileName.Split('.').Last(); uniqueFileName = Guid.NewGuid().ToString() + "." + stringCutted; itemCatagory.PhotoName1 = uniqueFileName; if (itemCatagoryViewModel.OldPhotoName.ToLower() == "no file") { fileUpload.Uploadfile("files/item_catagories", itemCatagoryViewModel.Photo, uniqueFileName); } else { fileUpload.Updatefile("files/item_catagories", itemCatagoryViewModel.Photo, itemCatagoryViewModel.OldPhotoName, uniqueFileName); } } try { _context.Update(itemCatagory); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ItemCatagoryExists(itemCatagory.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); }