public JsonResult CreateMealItem(MealItemViewModel mtvms) { IMealItemService _service = new MealItemService(); MealItem mealitem = Mapper.Map<MealItemViewModel, MealItem>(mtvms); foreach (var mealaller in mtvms.AllergenDD) { if (mealaller.Selected) { MealItems_AllergenicFoods aller = new MealItems_AllergenicFoods(); aller.AllergenicFoodID = mealaller.AllergenID; mealitem.MealItems_AllergenicFoods.Add(aller); } } mealitem.MealItemId = _service.AddAndReturnID(mealitem); return Json(new { success = true, id = mealitem.MealItemId }, JsonRequestBehavior.AllowGet); }
public ActionResult Create(MealItemViewModel mtvms, HttpPostedFileBase[] Photos) { try { if (ModelState.IsValid) { MealItem mealitem = Mapper.Map<MealItemViewModel, MealItem>(mtvms); foreach (var fileBase in Photos) { if (fileBase != null) { if (fileBase.ContentLength > 0) { var path = Path.Combine(Server.MapPath("~/MealPhotos"), WebSecurity.CurrentUserId + '-' + Path.GetRandomFileName().Replace(".", "").Substring(0, 8) + '-' + Path.GetFileName(fileBase.FileName)); fileBase.SaveAs(path); MealItems_Photos mp = new MealItems_Photos(); mp.Photo = path; mealitem.MealItems_Photos.Add(mp); } } } foreach (var mealaller in mtvms.AllergenDD) { if (mealaller.Selected) { MealItems_AllergenicFoods aller = new MealItems_AllergenicFoods(); aller.AllergenicFoodID = mealaller.AllergenID; mealitem.MealItems_AllergenicFoods.Add(aller); } } mealitem.MealItemId = _service.AddAndReturnID(mealitem); return RedirectToAction("Details", "MealItem", new { id = mealitem.MealItemId }); } } catch (Exception ex) { //Log the error (uncomment dex variable name after DataException and add a line here to write a log.) ModelState.AddModelError("", "Unable to save changes." + ex.Message.ToString()); } mtvms = PopulateDropDown(mtvms, null); return View(mtvms); }