public ActionResult Create(Product model) { try { model.Text = HttpUtility.HtmlDecode(model.Text); model.Id = 0; var category = _context.Categories.First(c => c.Id == model.CategoryId); var cache = new Product { Name = SiteHelper.UpdatePageWebName(model.Name), SortOrder = model.SortOrder, Category = category, Title = model.Title, Text = model.Text, IsContentPage = model.IsContentPage, }; for (int i = 0; i < Request.Files.Count; i++) { var file = Request.Files[i]; if (file == null) continue; if (string.IsNullOrEmpty(file.FileName)) continue; var pi = new ProductImage(); string fileName = IOHelper.GetUniqueFileName("~/Content/Images", file.FileName); string filePath = Server.MapPath("~/Content/Images"); filePath = Path.Combine(filePath, fileName); //file.SaveAs(filePath); GraphicsHelper.SaveOriginalImage(filePath, fileName, file); pi.ImageSource = fileName; cache.ProductImages.Add(pi); } _context.Products.Add(cache); var lang = _context.Languages.FirstOrDefault(p => p.Id == model.CurrentLang); if (lang != null) { CreateOrChangeContentLang(_context, model, cache, lang); } return RedirectToAction("Details", "Category", new { id = category.Id }); } catch { return View(model); } }
public ActionResult Create(FormCollection form, int categoryId, HttpPostedFileBase fileUpload) { using (var context = new SiteContainer()) { Product product = new Product(); Category category = context.Category.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", "Description" }); 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); fileUpload.SaveAs(filePath); product.ImageSource = fileName; } context.AddToProduct(product); context.SaveChanges(); return RedirectToAction("Index", "Catalogue", new { Area = "", id = category.Name }); } }
/// <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="imageSource">Initial value of the ImageSource property.</param> /// <param name="categoryId">Initial value of the CategoryId property.</param> public static Product CreateProduct(global::System.Int32 id, global::System.String title, global::System.String imageSource, global::System.Int32 categoryId) { Product product = new Product(); product.Id = id; product.Title = title; product.ImageSource = imageSource; product.CategoryId = categoryId; return product; }
private void CreateOrChangeContentLang(SiteContext context, Product instance, Product cache, Language lang) { ProductLang productLang = null; if (cache != null) { productLang = context.ProductLangs.FirstOrDefault(p => p.ProductId == cache.Id && p.LanguageId == lang.Id); } if (productLang == null) { var newPostLang = new ProductLang { ProductId = instance.Id, LanguageId = lang.Id, Title = instance.Title, Text = instance.Text }; context.ProductLangs.Add(newPostLang); } else { productLang.Title = instance.Title; productLang.Text = instance.Text; } context.SaveChanges(); }
/// <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); }