public async Task <IActionResult> Create() { var task = _dbContext.Categories.ToListAsync(); var model = new MenuItemAndCategoriesViewModel() { Item = new MenuItem() { ItemName = "hello this is first time test" }, Categories = await task }; return(View(model)); }
public async Task <IActionResult> Edit(int?id) { if (id == null) { return(NotFound()); } var categories = await _dbContext.Categories.ToListAsync(); var item = await _dbContext.MenuItems.FirstOrDefaultAsync(m => m.Id == id); var model = new MenuItemAndCategoriesViewModel() { Item = item, Categories = categories }; return(View(model)); }
public async Task <IActionResult> Create(MenuItemAndCategoriesViewModel menuItem) { if (!ModelState.IsValid) { menuItem.Categories = await _dbContext.Categories.ToListAsync(); return(View(menuItem)); } await _dbContext.MenuItems.AddAsync(menuItem.Item); await _dbContext.SaveChangesAsync(); var fileFromForm = Request.Form.Files; var webRoot = _hostingEnvironment.WebRootPath; var itemFromDb = await _dbContext.MenuItems.FindAsync(menuItem.Item.Id); if (fileFromForm.Any()) { var extension = Path.GetExtension(fileFromForm[0].FileName); var uploads = Path.Combine(webRoot, "images\\" + itemFromDb.Id + extension); await using (var fileStream = new FileStream(uploads, FileMode.Create)) { await fileFromForm[0].CopyToAsync(fileStream); } itemFromDb.Image = @"\images\" + itemFromDb.Id + extension; } else { var uploads = Path.Combine(webRoot, @"images\" + StaticData.defaultImage); System.IO.File.Copy(uploads, webRoot + @"\images\" + itemFromDb.Id + ".png"); itemFromDb.Image = @"\images\" + itemFromDb.Id + ".png"; } await _dbContext.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); }
public async Task <IActionResult> Edit(MenuItemAndCategoriesViewModel menuItem) { if (!ModelState.IsValid) { menuItem.Categories = await _dbContext.Categories.ToListAsync(); return(View(menuItem)); } var files = HttpContext.Request.Form.Files; var webRootPath = _hostingEnvironment.WebRootPath; var itemFromDb = await _dbContext.MenuItems.FindAsync(menuItem.Item.Id); itemFromDb.ItemName = menuItem.Item.ItemName; itemFromDb.CategoryId = menuItem.Item.CategoryId; itemFromDb.SubCategoryId = menuItem.Item.SubCategoryId; itemFromDb.description = menuItem.Item.description; itemFromDb.Price = menuItem.Item.Price; itemFromDb.Spicyness = menuItem.Item.Spicyness; if (files.Count > 0) { var upload = Path.Combine(webRootPath, "images"); var extensions = Path.GetExtension(files[0].FileName); await using (var fileStream = new FileStream(Path.Combine(upload, itemFromDb.Id + extensions) , FileMode.Create)) { var allMenuItemImages = new DirectoryInfo(Path.Combine(webRootPath, "images")).GetFiles(); await files[0].CopyToAsync(fileStream); } itemFromDb.Image = @"\images\" + itemFromDb.Id + extensions; } await _dbContext.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); }