public IActionResult Add(CheeseViewModel newCheeseVM, IFormFile photo) { if (ModelState.IsValid) { Cheese newCheese = new Cheese(); if (photo == null || photo.Length == 0) { newCheese.Photo = "image_tb.png"; } else { var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/images", photo.FileName); var stream = new FileStream(path, FileMode.Create); photo.CopyToAsync(stream); newCheese.Photo = photo.FileName; } newCheese.Name = newCheeseVM.Name; newCheese.Descriptions = newCheeseVM.Descriptions; newCheese.Country = newCheeseVM.Country; newCheese.Price = newCheeseVM.Price; newCheese.Quantity = newCheeseVM.Quantity; newCheese.Type = newCheeseVM.Type; _database.Cheeses.Add(newCheese); return(RedirectToAction("Index")); } else { return(View(newCheeseVM)); } }
public IActionResult Edit(int id, CheeseViewModel newCheese) { //CheeseData.Update(id, newCheese); var cheese = _database.Cheeses.FirstOrDefault(ch => ch.CheeseId == id); cheese.Name = newCheese.Name; cheese.Descriptions = newCheese.Descriptions; cheese.Country = newCheese.Country; cheese.Year = newCheese.Year; cheese.Price = newCheese.Price; cheese.Quantity = newCheese.Quantity; cheese.Type = newCheese.Type; cheese.CreateOn = DateTime.Now; ViewBag.status = 1; return(View(newCheese)); }
public IActionResult Edit(int id) { var cheese = _database.Cheeses.FirstOrDefault(ch => ch.CheeseId == id); CheeseViewModel cheeseViewModel = new CheeseViewModel() { Name = cheese.Name, Descriptions = cheese.Descriptions, Country = cheese.Country, Year = cheese.Year, Price = cheese.Price, Quantity = cheese.Quantity }; return(View(cheeseViewModel)); }
// GET: /<controller>/ public IActionResult Index() { var cheeses = _cheeseService.GetAllCheeses().ToArray(); var futureCheesePrices = _cheeseService.FutureCheesePrices(7); var message = ""; if (cheeses.Length == 0) { message = "There are no cheeses loaded yet, please load some cheeses"; } var viewModel = new CheeseViewModel { Cheeses = cheeses, FutureCheesePrices = futureCheesePrices, Message = message }; return(View(viewModel)); }
public IActionResult Add() { CheeseViewModel addCheeseViewModel = new CheeseViewModel(); return(View(addCheeseViewModel)); }