Telerik MVC 网格批量编辑
为什么我单击“保存”后我的更改不会保留到数据库中。
产品存储库:
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()));
}
}
Why wont my changes persist to db after I click save.
Product Repository:
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);
}
}
}
Controller:
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()));
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您似乎正在使用我们示例应用程序中的代码,该代码确实不会更新数据库。它只是更新内存中的数据,因此浏览演示的每个用户只能看到他的更改。
为了使其工作,您需要更新数据库。实际的实现取决于您用于数据访问的框架。例如,对于 Linq To SQL,您应该使用
SubmitChanges
方法。您可以检查 此代码库项目,用于更新底层数据库。You seem to be using the code from our sample application which indeed does not update the database. It merely updates the data in memory so every single user browsing the demos sees only his changes.
To make it work you need to update the database. The actual implementation depends on the framework you are using for data access. For example for Linq To SQL you should use the
SubmitChanges
method. You can check this code library project which updates the underlying database.