コード例 #1
0
        public ActionResult Destroy(IEnumerable<ProductViewModel> products)
        {
            using (var northwind = new NorthwindEntities())
            {
                //Iterate all destroyed products which are posted by the Kendo Grid
                foreach (var productViewModel in products)
                {
                    // Create a new Product entity and set its properties from productViewModel
                    var product = new Product
                    {
                        ProductID = (int)productViewModel.ProductID,
                    };

                    // Attach the entity
                    northwind.Products.Attach(product);
                    // Delete the entity
                    northwind.Products.DeleteObject(product);
                }

                // Delete the products from the database
                northwind.SaveChanges();

                //Return emtpy result
                return Json(null);
            }
        }
コード例 #2
0
        public ActionResult Create(IEnumerable<ProductViewModel> products)
        {
            var result = new List<Product>();

            using (var northwind = new NorthwindEntities())
            {
                //Iterate all created products which are posted by the Kendo Grid
                foreach (var productViewModel in products)
                {
                    // Create a new Product entity and set its properties from productViewModel
                    var product = new Product
                    {
                        ProductName = productViewModel.ProductName,
                        UnitPrice = productViewModel.UnitPrice,
                        UnitsInStock = productViewModel.UnitsInStock,
                        Discontinued = productViewModel.Discontinued
                    };

                    // store the product in the result
                    result.Add(product);

                    // Add the entity
                    northwind.Products.AddObject(product);
                }

                // Insert all created products to the database
                northwind.SaveChanges();

                // Return the inserted products - the Kendo Grid needs their ProductID which is generated by SQL server during insertion

                return Json(result.Select(p => new ProductViewModel
                {
                    ProductID = p.ProductID,
                    ProductName = p.ProductName,
                    UnitPrice = p.UnitPrice,
                    UnitsInStock = p.UnitsInStock,
                    Discontinued = p.Discontinued
                })
                .ToList());
            }
        }
コード例 #3
0
 /// <summary>
 /// Deprecated Method for adding a new object to the Products EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToProducts(Product product)
 {
     base.AddObject("Products", product);
 }
コード例 #4
0
 /// <summary>
 /// Create a new Product object.
 /// </summary>
 /// <param name="productID">Initial value of the ProductID property.</param>
 /// <param name="productName">Initial value of the ProductName property.</param>
 /// <param name="discontinued">Initial value of the Discontinued property.</param>
 public static Product CreateProduct(global::System.Int32 productID, global::System.String productName, global::System.Boolean discontinued)
 {
     Product product = new Product();
     product.ProductID = productID;
     product.ProductName = productName;
     product.Discontinued = discontinued;
     return product;
 }
コード例 #5
0
        public ActionResult Update(IEnumerable<ProductViewModel> products)
        {
            using (var northwind = new NorthwindEntities())
            {
                //Iterate all updated products which are posted by the Kendo Grid
                foreach (var productViewModel in products)
                {
                    // Create a new Product entity and set its properties from productViewModel
                    var product = new Product
                    {
                        ProductID = (int)productViewModel.ProductID,
                        ProductName = productViewModel.ProductName,
                        UnitPrice = productViewModel.UnitPrice,
                        UnitsInStock = productViewModel.UnitsInStock,
                        Discontinued = productViewModel.Discontinued
                    };

                    // Attach the entity
                    northwind.Products.Attach(product);
                    // Change its state to Modified so Entity Framework can update the existing product instead of creating a new one
                    northwind.ObjectStateManager.ChangeObjectState(product, EntityState.Modified);
                }

                // Save all updated products to the database
                northwind.SaveChanges();

                //Return emtpy result
                return Json(null);
            }
        }