// GET: ProviderPostIngredient/Create
 public ActionResult Create(string notificationId)
 {
     ProviderPostIngredientMV obj = new ProviderPostIngredientMV();
     obj.NotificationId = Int32.Parse(notificationId);
     ViewBag.UnitName = new SelectList(db.Units, "Name", "Name");
     return PartialView("_PostNewOffer", obj);
 }
        public ActionResult Create(ProviderPostIngredientMV model)
        {
            //if (ModelState.IsValid)
            //{
            try
                {
                    SaveChanges(model);
                    return Json(new { success = true });
                }
                catch (Exception e)
                {
                    ModelState.AddModelError("", e.Message);
                }

            //}

            ViewBag.UnitName = new SelectList(db.Units, "Name", "Name");
            //Something bad happened
            return PartialView("_PostNewOffer", model);
        }
        public void SaveChanges(ProviderPostIngredientMV model)
        {
            var userID =  System.Web.HttpContext.Current.User.Identity.GetUserId().ToString();

            var objIn = db.IngredientNotifies.Where(i => i.IngredientNotifyId == model.IngredientNotifyId).FirstOrDefault();
            var objIg = db.Ingredients.Where(r => r.Name == model.IngredientName).FirstOrDefault();
            var obju = db.RestaurantProviderRs.Where(r => r.providerId == userID).FirstOrDefault();

            var objIg2 = (objIg != null) ? objIg : new Ingredient() { Name = model.IngredientName, RestaurantId = obju.isFromRestaurantId };

            var objUn = db.Units.Where(x => x.Name == model.UnitName).FirstOrDefault();
            try
            {
                var objIngPost = new ProviderPostIngredient()
                {

                    IngredientNotify = (objIn != null)? objIn : new IngredientNotify() { NotificationId = model.NotificationId , DateCreated = DateTime.Now, Ingredient = objIg2, Unit = objUn },
                    Ingredient = objIg2,
                    Unit = db.Units.Where(y => y.Name == model.UnitName).FirstOrDefault(),
                    TotalRequired = model.TotalRequired.GetValueOrDefault(),
                    UnitCost = model.UnitCost.GetValueOrDefault(),
                    TotalCost = (model.UnitCost * model.TotalRequired).GetValueOrDefault(),
                    DateCreated = DateTime.Now,
                    DateExpired = model.DateExpired,
                    DateDelivery = model.DateExpired,
                    ProviderId = userID,
                    Notification = db.Notifications.Where(n=>n.NotificationId == model.NotificationId).FirstOrDefault()
                };

                db.ProviderPostIngredients.Add(objIngPost);
              db.SaveChanges();

             }
            catch (System.Data.Entity.Core.UpdateException e)
            {

            }
            catch (System.Data.Entity.Infrastructure.DbUpdateException ex) //DbContext
            {
                Console.WriteLine(ex.InnerException);
            }

            catch (Exception ex)
            {
                Console.WriteLine(ex.InnerException);
                throw;
            }

            // throw new Exception("Error test"); working Fine in Summary
        }