日期:2014-05-17  浏览次数:20572 次

ASP.NET MVC更新数据的问题
C# code

        public ActionResult Demo()
        {
            NorthwindEntity entity = new NorthwindEntity();
            IQueryable<Products> pro = from s in entity.Products
                                       where s.ProductID == 1
                                       select s;
            return View(pro);
        }
        
        //为什么用这个方法不能更新实体类库里的Products数据呢,请问哪里出错啊,请大牛帮忙,谢谢!
        public ActionResult UpdateDemo(Products pd)
        {
            NorthwindEntity entity = new NorthwindEntity();
            Products model = entity.Products.FirstOrDefault(e => e.ProductID == pd.ProductID);
            UpdateModel(model, pd.ProductName);
            entity.SaveChanges();
            return RedirectToAction("Demo");
        }



------解决方案--------------------
[AcceptVerbs(HttpVerbs.Post)]//增加这句看看
public ActionResult UpdateDemo(Products pd)
{
NorthwindEntity entity = new NorthwindEntity();
Products model = entity.Products.FirstOrDefault(e => e.ProductID == pd.ProductID);
UpdateModel(model, pd.ProductName);
entity.SaveChanges();
return RedirectToAction("Demo");
}

------解决方案--------------------
帮顶