コード例 #1
0
 public ActionResult Create(Commodity model)
 {
     if (ModelState.IsValid)
     {
         db.Commodities.Add(model);
         db.SaveChanges();
         return RedirectToAction("Index");
     }
     SelectList categories = new SelectList(db.Categories.ToList(), "CategoryID", "CategoryName", db.Categories.ToList()[0]);//获得商品种类信息,传给视图
     ViewBag.CategoryID = categories;
     return View(model);
 }
コード例 #2
0
        public ActionResult Edit(Commodity model)
        {
            try
            {
                Commodity commodity = db.Commodities.Find(model.CommodityID);
                UpdateModel(commodity);
                db.SaveChanges();
                return RedirectToAction("Details", new { id = model.CommodityID });

            }
            catch (Exception)
            {
                ModelState.AddModelError("", "修改错误!");

            }
            SelectList categories = new SelectList(db.Categories.ToList(), "CategoryID", "CategoryName", db.Categories.ToList()[0]);    //获得商品种类信息,传给视图
            ViewBag.CategoryID = categories;
            return View(model);
        }