public IActionResult CreateFood(Foodviewmode food) { var userinfo = JsonConvert.DeserializeObject <Mess>(HttpContext.Session.GetString("sessionUser1234")); int id = userinfo.MessId; if (ModelState.IsValid) { string uniqueFileName = Photopathfood(food); Food newfood = new Food { FoodName = food.foodName, foodCalories = food.foodCalories, photopath = uniqueFileName, foodCategoryId = food.foodCategoryId, Price = food.price }; //if (Image != null) _Context.Add(newfood); _Context.SaveChanges(); return(RedirectToAction(nameof(FoodList))); } ViewBag.foodCategoryId = new SelectList(_Context.foodCategories.Where(m => m.MessId == id), "FoodCategoryId", "FoodCategoryName", food.foodCategoryId); return(View(food)); }
/// <summary> ///return edit view /// </summary> /// <param name="id"></param> /// <returns></returns> public async Task <IActionResult> EditView(int?id) { if (id == null) { return(NotFound()); } //photopath var food = await _Context.food.FindAsync(id); TempData["pic"] = food.photopath; if (food == null) { return(NotFound()); } //IFormFile Foodviewmode newfood = new Foodviewmode { FoodId = food.FoodId, foodName = food.FoodName, foodCalories = food.foodCalories, foodCategoryId = food.foodCategoryId, price = food.Price }; var userinfo = JsonConvert.DeserializeObject <Mess>(HttpContext.Session.GetString("sessionUser1234")); int idf = userinfo.MessId; ViewBag.foodCategoryId = new SelectList(_Context.foodCategories.Where(m => m.MessId == idf), "FoodCategoryId", "FoodCategoryName"); return(View(newfood)); }
public IActionResult CreateFood(Foodviewmode food) { if (ModelState.IsValid) { string uniqueFileName = Photopathfood(food); myfoods newfood = new myfoods { myfoodsName = food.foodName, foodCalories = food.foodCalories, photopath = uniqueFileName, Price = food.price, photopath11 = "jjjjj" }; //if (Image != null) _Context.Add(newfood); _Context.SaveChanges(); return(RedirectToAction(nameof(FoodList))); } return(View(food)); }
/// <summary> /// return photopath /// </summary> /// <param name="food"></param> /// <returns>photopath </returns> public string Photopathfood(Foodviewmode food) { string uniqueFileName = null; string paths = null; if (food.photo != null) { string uploadFolder = Path.Combine(HostingEnvironment.WebRootPath, "images"); uniqueFileName = Guid.NewGuid().ToString() + "_" + food.photo.FileName; string filePath = Path.Combine(uploadFolder, uniqueFileName); food.photo.CopyTo(new FileStream(filePath, FileMode.Create)); } ; return(uniqueFileName); }
public async Task <IActionResult> EditView(int id, Foodviewmode food) { string uniqueFileName; var userinfo = JsonConvert.DeserializeObject <Mess>(HttpContext.Session.GetString("sessionUser1234")); int idd = userinfo.MessId; if (id != food.FoodId) { return(NotFound()); } if (ModelState.IsValid) { if (food.photo == null) { uniqueFileName = TempData["pic"].ToString(); } else { uniqueFileName = Photopathfood(food); } // Photopathfood(food); // photo = TempData["pic"].ToString(); Food newfood1 = new Food { FoodId = food.FoodId, FoodName = food.foodName, foodCalories = food.foodCalories, photopath = uniqueFileName, foodCategoryId = food.foodCategoryId, Price = food.price, }; _Context.Update(newfood1); await _Context.SaveChangesAsync(); return(RedirectToAction(nameof(FoodList))); } ViewBag.foodCategoryId = new SelectList(_Context.foodCategories.Where(m => m.MessId == idd), "FoodCategoryId", "FoodCategoryName"); return(View(food)); }