public ActionResult Create(FormCollection form, HttpPostedFileBase uploadFile) { using (var context = new ModelContainer()) { int albumId = Convert.ToInt32(form["AlbumId"]); var album = context.Album.First(a => a.Id == albumId); PostCheckboxesData cbDataCategories = form.ProcessPostCheckboxesData("category"); PostCheckboxesData cbDataElements = form.ProcessPostCheckboxesData("element"); var product = new Product(); TryUpdateModel(product, new[] { "Title", "SortOrder" }); string fileName = IOHelper.GetUniqueFileName("~/Content/Images", uploadFile.FileName); string filePath = Server.MapPath("~/Content/Images"); filePath = Path.Combine(filePath, fileName); uploadFile.SaveAs(filePath); product.ImageSource = fileName; foreach (var kvp in cbDataCategories) { var categoryId = kvp.Key; bool productValue = kvp.Value; if (productValue) { var category = context.Category.First(c => c.Id == categoryId); product.Categories.Add(category); } } foreach (var kvp in cbDataElements) { var elementId = kvp.Key; var elemrntValue = kvp.Value; if (elemrntValue) { var element = context.Element.First(e => e.Id == elementId); product.Elements.Add(element); } } album.Products.Add(product); context.SaveChanges(); return RedirectToAction("Index", "Albums", new { Area = "", id = album.Name }); } }
/// <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="title">Initial value of the Title property.</param> /// <param name="albumId">Initial value of the AlbumId property.</param> /// <param name="imageSource">Initial value of the ImageSource property.</param> public static Product CreateProduct(global::System.Int32 id, global::System.String title, global::System.Int32 albumId, global::System.String imageSource) { Product product = new Product(); product.Id = id; product.Title = title; product.AlbumId = albumId; product.ImageSource = imageSource; return product; }