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);
        }
        private MealItemViewModel PopulateDropDown(MealItemViewModel mtvm, MealItem mealitem)
        {
            IMealItemService _service = new MealItemService();
            if (mtvm == null)
                mtvm = new MealItemViewModel();
            mtvm.ServingUnitDDList = _service.GetServingUnitDDList().Select(x => new SelectListItem
            {
                Value = x.ServingUnitID.ToString(),
                Text = x.ServingUnit,
                Selected = (mealitem != null && mealitem.ServingUnit == x.ServingUnitID)
            });

            mtvm.MealTypeDD.MealTypeDDList = _service.MealTypeDDList().Select(x => new SelectListItem
            {
                Value = x.MealTypeID.ToString(),
                Text = x.Name,
                Selected = (mealitem != null && mealitem.MealTypeID == x.MealTypeID)
            });
            mtvm.CusineTypeDD.CuisineDDList = _service.CuisineTypeDDList().Select(x => new SelectListItem
            {
                Value = x.CuisineTypeID.ToString(),
                Text = x.Name,
                Selected = (mealitem != null && mealitem.CusineTypeID == x.CuisineTypeID)
            });

            mtvm.DietTypeDD.DietTypeDDList = _service.DietTypeDDList().Select(x => new SelectListItem
            {
                Value = x.DietTypeID.ToString(),
                Text = x.Name,
                Selected = (mealitem != null && mealitem.DietTypeID == x.DietTypeID)
            });
            mtvm.AllergenDD = _service.AllergenicFoodsDDList().Select(x => new Allergen
            {
                AllergenName = x.AllergenicFood,
                AllergenID = x.AllergenicFoodID,
                Selected = (mealitem != null && mealitem.MealItems_AllergenicFoods.Where(y => y.AllergenicFoodID == x.AllergenicFoodID).Count() > 0)
            }).ToList();

            return mtvm;
        }
 public JsonResult CreateMealItem1()
 {
     MealItemViewModel MealAdvm = new MealItemViewModel();
     MealAdvm.AllergenDD = new List<Allergen>();
     MealAdvm.AllergenDD.Add(new Allergen());
     //MealAdvm.AvailabilityTypeDD = new AvailabilityTypeViewModel();
     return Json(MealAdvm, JsonRequestBehavior.AllowGet);
 }
        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 Edit(MealItemViewModel mtvms, HttpPostedFileBase[] Photos)
        {
            if (ModelState.IsValid)
            {
                MealItem mealitem132 = Mapper.Map<MealItemViewModel, MealItem>(mtvms);
                MealItem mealitem = db.MealItems.Where(x => x.MealItemId == mtvms.MealItemId).FirstOrDefault();
                mealitem.MealItemName = mtvms.MealItemName;
                mealitem.Ingredients = mtvms.Ingredients;
                mealitem.DietTypeID = mealitem132.DietTypeID;
                mealitem.MealTypeID = mealitem132.MealTypeID;
                mealitem.CusineTypeID = mealitem132.CusineTypeID;
                mealitem.ServingUnit = mealitem132.ServingUnit;
                mealitem.Quantity = mtvms.Quantity;
                mealitem.Status = mtvms.Status;
                mealitem.Price = mtvms.Price;

                List<MealItems_AllergenicFoods> lstMealItems_AllergenicFoods = new List<MealItems_AllergenicFoods>();
                foreach (var allergenic in mealitem.MealItems_AllergenicFoods)
                    lstMealItems_AllergenicFoods.Add(allergenic);
                foreach (var allergenic in lstMealItems_AllergenicFoods)
                {
                    //mealitem.MealItems_AllergenicFoods.Remove(allergenic);

                    db.MealItems_AllergenicFoods.Remove(allergenic);

                    db.Entry(mealitem).State = EntityState.Modified;
                    db.SaveChanges();
                }

                //db.SaveChanges();
                foreach (var allergenic in mtvms.AllergenDD.Where(x => x.Selected))
                {
                    mealitem.MealItems_AllergenicFoods.Add(new MealItems_AllergenicFoods() { AllergenicFoodID = allergenic.AllergenID, MealItemID = mealitem.MealItemId });

                }

                if (Photos != null)
                {
                    foreach (var fileBase in Photos)
                    {
                        if (fileBase != null)
                        {
                            List<MealItems_Photos> lstMealItems_Photos = new List<MealItems_Photos>();
                            foreach (var photo in mealitem.MealItems_Photos)
                            {
                                lstMealItems_Photos.Add(photo);
                            }
                            //foreach (var photo in lstMealItems_Photos)
                            //{
                            //    db.MealItems_Photos.Remove(photo);

                            //    db.Entry(mealitem).State = EntityState.Modified;
                            //    db.SaveChanges();
                            //}
                            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);
                            }
                        }

                    }
                }

                db.Entry(mealitem).State = EntityState.Modified;
                db.SaveChanges();
                //MealItem mealitem = Mapper.Map<MealItemViewModel, MealItem>(mtvms);
                //_service.Update(mealitem);
                return RedirectToAction("Index", new { userID = WebSecurity.CurrentUserId });
            }
            else
            {
                //MealItem mealitem = _service.GetById(id);
                //MealItemViewModel mtvm = Mapper.Map<MealItem, MealItemViewModel>(mealitem);

                mtvms = PopulateDropDown(mtvms, null);
                return View(mtvms);
            }
            //return View(mtvms);
        }