public ActionResult Create(FormCollection form, int categoryId, HttpPostedFileBase fileUpload) { using (var context = new StructureContainer()) { Product product = new Product(); Category category = context.Category.Include("Parent").First(c => c.Id == categoryId); product.Category = category; var attributes = context.ProductAttribute.ToList(); PostCheckboxesData postData = form.ProcessPostCheckboxesData("attr", "categoryId"); foreach (var kvp in postData) { var attribute = attributes.First(a => a.Id == kvp.Key); if (kvp.Value) { if (!product.ProductAttributes.Contains(attribute)) product.ProductAttributes.Add(attribute); } else { if (product.ProductAttributes.Contains(attribute)) product.ProductAttributes.Remove(attribute); } } TryUpdateModel(product, new[] { "Title", "TitleEng", "Description", "DescriptionEng", "ShowOnMainPage","Discount","DiscountText" }); if (fileUpload != null) { string fileName = IOHelper.GetUniqueFileName("~/Content/Images", fileUpload.FileName); string filePath = Server.MapPath("~/Content/Images"); filePath = Path.Combine(filePath, fileName); GraphicsHelper.SaveOriginalImage(filePath, fileName, fileUpload); product.ImageSource = fileName; } context.AddToProduct(product); context.SaveChanges(); return category.Parent != null ? RedirectToAction("Index", "Catalogue", new { Area = "", category = category.Parent.Name, subCategory = category.Name }) : RedirectToAction("Index", "Catalogue", new { Area = "", category = category.Name }); } }
/// <summary> /// Create a new Product object. /// </summary> /// <param name="id">Initial value of the Id property.</param> /// <param name="categoryId">Initial value of the CategoryId property.</param> /// <param name="imageSource">Initial value of the ImageSource property.</param> /// <param name="discount">Initial value of the Discount property.</param> public static Product CreateProduct(global::System.Int32 id, global::System.Int32 categoryId, global::System.String imageSource, global::System.Boolean discount) { Product product = new Product(); product.Id = id; product.CategoryId = categoryId; product.ImageSource = imageSource; product.Discount = discount; return product; }
public ActionResult CreateMany(FormCollection form, int categoryId, IList<HttpPostedFileBase> fileUpload, IList<string> titleRU, IList<string> titleEN, IList<string> descriptionRU, IList<string> descriptionEN) { using (var context = new StructureContainer()) { Category category = context.Category.Include("Parent").First(c => c.Id == categoryId); var attributes = context.ProductAttribute.ToList(); PostCheckboxesData postData = form.ProcessPostCheckboxesData("attr", "categoryId"); for (int i = 0; i < 10; i++) { if (fileUpload[i] != null) { Product product = new Product {Category = category}; string fileName = IOHelper.GetUniqueFileName("~/Content/Images", fileUpload[i].FileName); string filePath = Server.MapPath("~/Content/Images"); filePath = Path.Combine(filePath, fileName); GraphicsHelper.SaveOriginalImage(filePath, fileName, fileUpload[i]); product.ImageSource = fileName; foreach (var kvp in postData) { var attribute = attributes.First(a => a.Id == kvp.Key); if (kvp.Value) { if (!product.ProductAttributes.Contains(attribute)) product.ProductAttributes.Add(attribute); } else { if (product.ProductAttributes.Contains(attribute)) product.ProductAttributes.Remove(attribute); } } product.Title = titleRU[i]; product.TitleEng = titleEN[i]; product.Description = descriptionRU[i]; product.DescriptionEng = descriptionEN[i]; context.AddToProduct(product); } } context.SaveChanges(); return category.Parent != null ? RedirectToAction("Index", "Catalogue", new { Area = "", category = category.Parent.Name, subCategory = category.Name }) : RedirectToAction("Index", "Catalogue", new { Area = "", category = category.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); }