public ActionResult Insert(Product product) { var northwind = new Models.NorthwindEntities(); var products = northwind.Products; var categories = northwind.Categories; // Find the category which has been chosen using the posted Category.CategoryID (specified using the dropdownlist) var category = categories.FirstOrDefault(c => c.CategoryID == product.Category.CategoryID); // Set the chosen Category product.Category = category; // Add the new product products.AddObject(product); // Save the changes in the database northwind.SaveChanges(); var data = from p in products select new // Use anonymous type to avoid JSON serialization exceptions due to circular object references. Also serialize only the required properties (for performance) { p.Category, p.ProductID, p.ProductName, p.CategoryID }; return View(new GridModel(data)); }
public ActionResult Delete(Product product) { var northwind = new Models.NorthwindEntities(); var products = northwind.Products; // Get the product being deleted using the posted ProductID var actualProduct = products.FirstOrDefault(p => p.ProductID == product.ProductID); if (actualProduct != null) { // Delete the product products.DeleteObject(actualProduct); // Save the changes in the database northwind.SaveChanges(); } var data = from p in products select new // Use anonymous type to avoid JSON serialization exceptions due to circular object references. Also serialize only the required properties (for performance) { p.Category, p.ProductID, p.ProductName, p.CategoryID }; return View(new GridModel(data)); }
/// <summary> /// Deprecated Method for adding a new object to the Products EntitySet. Consider using the .Add method of the associated ObjectSet<T> property instead. /// </summary> public void AddToProducts(Product product) { base.AddObject("Products", product); }
/// <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; }
partial void DeleteProduct(Product instance);
partial void UpdateProduct(Product instance);
partial void InsertProduct(Product instance);
private void detach_Products(Product entity) { this.SendPropertyChanging(); entity.Category = null; }
private void attach_Products(Product entity) { this.SendPropertyChanging(); entity.Category = this; }