コード例 #1
0
        public ActionResult AddIngredients(/*int? id*/)
        {
            //if (id == null)
            //{
            //    return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            //}

            var pid = (string)Session["id"];
            var id  = Convert.ToInt16(pid);

            var Ingredients = new DreamProdIngredientsViewModel
            {
                DreamProduct = db.dreamProducts.Include(i => i.Supplies).First(i => i.Id == id),
            };

            if (Ingredients.DreamProduct == null)
            {
                return(HttpNotFound());
            }

            var inglist = db.supplies.ToList();

            Ingredients.SupplyList = inglist.Select(o => new SelectListItem
            {
                Text  = o.SupplyName,
                Value = o.SupplyId.ToString()
            });

            //ViewBag.Employee =
            //        new SelectList(db.Users, "Id", "FullName", Ingredients.DailyTask.Name);

            return(View(Ingredients));
        }
コード例 #2
0
        [ValidateAntiForgeryToken]//[Bind(Include = "Title,Id,EmployerID,SelectedJobTags")]
        public ActionResult AddIngredients(DreamProdIngredientsViewModel ingView)
        {
            if (ingView == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }



            if (ModelState.IsValid)
            {
                var ToUpdate = db.dreamProducts
                               .Include(i => i.Supplies).First(i => i.Id == ingView.DreamProduct.Id);

                if (TryUpdateModel(ToUpdate, "DreamProducts", new string[] { "ProdName", "Type", "Description", "UserName" }))
                {
                    var newIngredients = db.supplies.Where(
                        m => ingView.supplies.Contains(m.SupplyId)).ToList();
                    var updatedIngredients = new HashSet <int>(ingView.supplies);
                    foreach (Supplies supplies in db.supplies)
                    {
                        if (!updatedIngredients.Contains(supplies.SupplyId))
                        {
                            ToUpdate.Supplies.Remove(supplies);
                        }
                        else
                        {
                            ToUpdate.Supplies.Add((supplies));
                        }
                    }

                    ApplicationDbContext cont = new ApplicationDbContext();
                    var list = new List <DreamProdIngredients>();
                    foreach (var item in newIngredients)
                    {
                        list.Add(new DreamProdIngredients
                        {
                            SupplyId    = item.SupplyId,
                            SupplyName  = item.SupplyName,
                            DreamProdId = ToUpdate.Id
                        });
                        db.dreamProdIngredients.AddRange(list);
                    }

                    db.Entry(ToUpdate).State = System.Data.Entity.EntityState.Modified;
                    db.SaveChanges();
                }

                return(RedirectToAction("Successful"));
                //return RedirectToAction("Index");
            }
            //ViewBag.Employee = new SelectList(db.Users, "Id", "FullName", ingView.DailyTask.Name);
            return(View(ingView));
        }