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

后端 未结 7 2045
慢半拍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-14 00:08

    Try the followings. The idea is that product param that MVC model binding to the Action method is not syn'ing with EF, so we need to assocaite it to the repository:

    public ActionResult Edit(Product product)   
    {
        if (ModelState.IsValid)
        {
            ((ObjectSet)repository.Products).ApplyCurrentValues(product);
    
            repository.SaveProduct(product);
            TempData["message"] = string.Format("{0} has been saved", product.Name);
            return RedirectToAction("Index");
        }
        else 
        {
            return View(product);
        }
    }
    

提交回复
热议问题