// GET: Admin/FoodItemVMs/Create public ActionResult Create() { //Init the model FoodItemVM model = new FoodItemVM(); model.FoodTypes = new SelectList(db.FoodTypes.ToList(), "Id", "Name"); //Return model with view return(View(model)); }
public IActionResult ConfirmDeleteFoodItem(int id) { var delFoodItem = NHibernateHelperCore.GetSingleOrDefault <FoodItem>(a => a.Id == id); var foodItemVM = new FoodItemVM() { Name = delFoodItem.Name, Id = delFoodItem.Id, Price = delFoodItem.Price, FoodCategoryId = delFoodItem.FoodCategory.Id }; return(View(foodItemVM)); }
public IActionResult CreateOrUpdateFoodItem(FoodItemVM foodItemVm) { var foodCategory = NHibernateHelperCore.GetSingleOrDefault <FoodCategory>(a => a.Id == foodItemVm.FoodCategoryId); var foodItem = NHibernateHelperCore.GetSingleOrDefault <FoodItem>(a => a.Id == foodItemVm.Id) ?? new FoodItem() { Name = foodItemVm.Name, Id = foodItemVm.Id, Price = foodItemVm.Price, FoodCategory = foodCategory }; foodItem.Name = foodItemVm.Name; foodItem.Price = foodItemVm.Price; NHibernateHelperCore.SaveOrUpdate(foodItem); return(RedirectToAction("Index", "FoodItem", new { id = foodItemVm.FoodCategoryId })); }
public ActionResult Create(FoodItemVM model, HttpPostedFileBase file) { //Make sure product name unique if (db.FoodItems.Any(x => x.FoodName == model.FoodName)) { model.FoodTypes = new SelectList(db.FoodTypes.ToList(), "Id", "Name"); ModelState.AddModelError("", "That product name is already taken!"); return(View(model)); } var originalDirectory = new DirectoryInfo(string.Format("{0}Content\\Images", Server.MapPath(@"\\"))); var pathString1 = Path.Combine(originalDirectory.ToString(), "Products"); if (!Directory.Exists(pathString1)) { Directory.CreateDirectory(pathString1); } //Check if a file was uploaded if (file != null && file.ContentLength > 0) { //Get file extension string ext = file.ContentType.ToLower(); //verify extension if (ext != "image/jpg" && ext != "image/jpeg" && ext != "image/png" && ext != "image/pjpeg") { ModelState.AddModelError("", "That Image was not uploaded, wrong format."); return(View(model)); } //init image name string imageName = file.FileName; //set original and thumb image paths var path = string.Format("{0}\\{1}", pathString1, imageName); //save original image file.SaveAs(path); //create and save thumb } int id; //init and save productDTO int dd = Convert.ToInt32(model.FoodTypeName); FoodType type = db.FoodTypes.FirstOrDefault(x => x.Id == dd); FoodItem food = new FoodItem(); food.FoodName = model.FoodName; food.FoodTypeName = type.Name; food.Price = model.Price; food.Image = file.FileName; db.FoodItems.Add(food); db.SaveChanges(); //get the id id = food.Id; //save tempdata message TempData["SM"] = "You have added a product"; return(RedirectToAction("Create")); }