Telerik MVC 网格批量编辑

发布于 2024-12-04 20:41:01 字数 3624 浏览 0 评论 0原文

为什么我单击“保存”后我的更改不会保留到数据库中。

产品存储库:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如何视而不见 2024-12-11 20:41:01

您似乎正在使用我们示例应用程序中的代码,该代码确实不会更新数据库。它只是更新内存中的数据,因此浏览演示的每个用户只能看到他的更改。

为了使其工作,您需要更新数据库。实际的实现取决于您用于数据访问的框架。例如,对于 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文