public ActionResult CreateMenu(LunchMenu menuObj) { LunchMenuModel model = null; if (ModelState.IsValid) { try { if (menuObj.ImagePath != null) { string fileName = Saveimage(menuObj); model = new LunchMenuModel { DishName = menuObj.DishName, Price = menuObj.Price, ServedAt = menuObj.ServedAt, ImagePath = fileName }; } using (MongoContext<LunchMenuModel> db = new MongoContext<LunchMenuModel>("LunchMenu")) { db.context.Insert(model); return RedirectToAction("Index"); } } catch (Exception e) { ViewBag.Error = e.Message; return View(); } } return View(); }
private string Saveimage(LunchMenu menuObj) { try { var fileName = Guid.NewGuid().ToString() + Path.GetFileName(menuObj.ImagePath.FileName); var path = Path.Combine(Server.MapPath("~/images"), fileName); menuObj.ImagePath.SaveAs(path); return fileName; } catch (Exception e) { return string.Empty; } }