public ActionResult Create(FormCollection form, int brandId, IEnumerable<HttpPostedFileBase> fileUpload, IList<string> fileTitles, string filter) { try { using (var context = new CatalogueContainer()) { var brand = context.Brand.Include("Category").First(b => b.Id == brandId); foreach (var file in fileUpload) { if (file != null) { var product = new Product { Brand = brand }; string fileName = IOHelper.GetUniqueFileName("~/Content/Images", file.FileName); string filePath = Server.MapPath("~/Content/Images"); filePath = Path.Combine(filePath, fileName); file.SaveAs(filePath); product.ImageSource = fileName; context.AddToProduct(product); } } context.SaveChanges(); return RedirectToAction("Index", "Catalogue", new { Area = "", brand = brand.Name, category=brand.Category.Name, filter = filter }); } } catch { return View(); } }
/// <summary> /// Deprecated Method for adding a new object to the Product EntitySet. Consider using the .Add method of the associated ObjectSet<T> property instead. /// </summary> public void AddToProduct(Product product) { base.AddObject("Product", product); }
/// <summary> /// Create a new Product object. /// </summary> /// <param name="id">Initial value of the Id property.</param> /// <param name="imageSource">Initial value of the ImageSource property.</param> /// <param name="brandId">Initial value of the BrandId property.</param> public static Product CreateProduct(global::System.Int32 id, global::System.String imageSource, global::System.Int32 brandId) { Product product = new Product(); product.Id = id; product.ImageSource = imageSource; product.BrandId = brandId; return product; }