public ActionResult Create() { var viewModel = new CreateMealViewModel() { Categories = categoryRepo.GetWithFilterAndOrder() }; return View(viewModel); }
public ActionResult Create(CreateMealViewModel viewModel) { HttpPostedFileBase hpf = Request.Files[0] as HttpPostedFileBase; if (hpf == null || hpf.ContentLength == 0) { ModelState.AddModelError("", "並未選取圖片"); } if (ModelState.IsValid) { Meal meal = new Meal() { CategoryId = viewModel.CategoryId, MealName = viewModel.MealName, Price = viewModel.Price, //TODO: For now only one supplier SupplierId = 1 }; { string savedFileName = Guid.NewGuid().ToString() + "." + (hpf.FileName.Split('.')).Last(); string path = Path.Combine(Server.MapPath("~/Content/ProductImg"), savedFileName); hpf.SaveAs(path); meal.Image = savedFileName; } mealRepo.Insert(meal); mealRepo.SaveChanges(); return RedirectToAction("Index"); } viewModel.Categories = categoryRepo.GetWithFilterAndOrder(); return View(viewModel); }
public ActionResult Edit(CreateMealViewModel viewModel) { HttpPostedFileBase hpf = Request.Files[0] as HttpPostedFileBase; if (ModelState.IsValid) { Meal meal = mealRepo.GetSingleEntity(x => x.MealId == viewModel.MealId); meal.CategoryId = viewModel.CategoryId; meal.MealName = viewModel.MealName; meal.Price = viewModel.Price; if (hpf != null && hpf.ContentLength != 0) { string savedFileName = Guid.NewGuid().ToString() + "." + (hpf.FileName.Split('.')).Last(); string path = Path.Combine(Server.MapPath("~/Content/ProductImg"), savedFileName); hpf.SaveAs(path); meal.Image = savedFileName; } mealRepo.Update(meal); mealRepo.SaveChanges(); return RedirectToAction("Index"); } viewModel.Categories = categoryRepo.GetWithFilterAndOrder(); return View(viewModel); }
public ActionResult Edit(int id = 0) { Meal meal = mealRepo.GetSingleEntity(x => x.MealId == id); var viewModel = new CreateMealViewModel() { Categories = categoryRepo.GetWithFilterAndOrder(), MealName = meal.MealName, CategoryId = meal.CategoryId, Price = meal.Price, Image = meal.Image, MealId = meal.MealId }; if (meal == null) { return HttpNotFound(); } return View(viewModel); }