2011-09-16 127 views
0

爲什麼我的更改在我點擊保存後仍然保留到db。Telerik MVC Grid批量編輯

產品資源庫:

public class ProductRepository 
{ 
    NorthwindDataContext context = new NorthwindDataContext(); 

    public IList<EditableProduct> All() 
    { 
     return (from product in context.Products 
       select new EditableProduct { 
        ProductID = product.ProductID, 
        ProductName = product.ProductName, 
        UnitPrice = product.UnitPrice.HasValue ? product.UnitPrice.Value : default(decimal), 
        UnitsInStock = product.UnitsInStock.HasValue ? product.UnitsInStock.Value : default(int), 
        Discontinued = product.Discontinued, 
        LastSupply = DateTime.Today 
       }).ToList(); 
    } 

    public EditableProduct One(Func<EditableProduct, bool> predicate) 
    { 
     return All().Where(predicate).FirstOrDefault(); 
    } 

    public void Update(EditableProduct product) 
    { 
     EditableProduct target = One(p => p.ProductID == product.ProductID); 

     if (target != null) 
     { 
      target.ProductName = product.ProductName; 
      target.UnitPrice = product.UnitPrice; 
      target.UnitsInStock = product.UnitsInStock; 
      target.Discontinued = product.Discontinued; 
      target.LastSupply = product.LastSupply; 
     } 
    } 

    public void Insert(EditableProduct product) 
    { 
     product.ProductID = All().OrderByDescending(p => p.ProductID).First().ProductID + 1; 
     All().Insert(0, product); 
    } 

    public void Delete(EditableProduct product) 
    { 
     EditableProduct target = One(p => p.ProductID == product.ProductID); 
     if (target != null) 
     { 
      All().Remove(target); 
     } 
    } 
} 

控制器:

public partial class GridController : Controller 
{ 
    ProductRepository productRepository = new ProductRepository(); 

    public ActionResult EditingBatch() 
    { 
     return View(); 
    } 

    [HttpPost] 
    [GridAction] 
    public ActionResult _SelectBatchEditing() 
    { 
     return View(new GridModel(productRepository.All())); 
    } 

    [HttpPost] 
    [GridAction] 
    public ActionResult _SaveBatchEditing([Bind(Prefix = "inserted")]IEnumerable<EditableProduct> insertProducts, 
     [Bind(Prefix = "updated")]IEnumerable<EditableProduct> updatedProducts, 
     [Bind(Prefix = "deleted")]IEnumerable<EditableProduct> deletedProducts) 
    { 
     if (insertProducts != null) 
     { 
      foreach (var product in insertProducts) 
      { 
       productRepository.Insert(product); 
      } 
     } 
     if (updatedProducts != null) 
     { 
      foreach (var product in updatedProducts) 
      { 
       var target = productRepository.One(p => p.ProductID == product.ProductID); 
       if (target != null) 
       { 
        target.ProductName = product.ProductName; 
        target.UnitPrice = product.UnitPrice; 
        target.UnitsInStock = product.UnitsInStock; 
        target.LastSupply = product.LastSupply; 
        target.Discontinued = product.Discontinued; 

        productRepository.Update(target); 
       } 
      } 
     } 
     if (deletedProducts != null) 
     { 
      foreach (var product in deletedProducts) 
      { 
       productRepository.Delete(product); 
      } 
     } 
     return View(new GridModel(productRepository.All())); 
    } 
} 

回答

0

你似乎使用的代碼來自這的確不更新數據庫示例應用程序。它僅更新內存中的數據,因此每個瀏覽演示的用戶都只能看到他的更改。

要使其工作,您需要更新數據庫。實際的實現取決於您用於數據訪問的框架。例如,對於Linq To SQL,您應該使用SubmitChanges方法。您可以檢查this更新底層數據庫的代碼庫項目。