Apress Pro Asp.net MVC Framework 3 - SportsStore Edit Product not working?

后端 未结 7 2061
慢半拍i
慢半拍i 2021-02-13 23:33

G’day All, has anyone purchased the ALPHA of Apress Pro Asp.net MVC Framework 3 and created the SportsStore? I can’t for the life of me edit products and have the DB update suc

7条回答
  •  独厮守ぢ
    2021-02-13 23:55

    try this

     public ActionResult Edit(Product product)
        {
            if (ModelState.IsValid)
            {
                Product p = repository.Products.FirstOrDefault(x => x.ProductID == product.ProductID);
                if (p != null)
                {
                    p.ProductID = product.ProductID;
                    p.Price = product.Price;
                    p.Category = product.Category;
                    p.Description = product.Description;
                    p.Name = product.Name;
                }
                else
                    p = product;
                repository.SaveProduct(p);
                TempData["message"] = string.Format("{0} has been saved", product.Name);
                return RedirectToAction("Index");
            }
            else 
            {
                return View(product);
            }
        }
    

    im beginner with mvc 3, but i think the model reference (product parameter) is a Product object that is not bind to EF Context

提交回复
热议问题