public WorksModel(SiteContainer context, int productId) : base(context, "gallery") { _context = context; Product = _context.Product.Include("Category").Include("ProductSizes").Include("ProductImages").First(p => p.Id == productId); Category = Product.Category; }
public ActionResult Create(int id) { using (var context = new SiteContainer()) { var category = context.Category.First(c => c.Id == id); var product = new Product { Category = category }; //ViewBag.Categories = categories.Select(category => new SelectListItem {Text = category.Title, Value = category.Id.ToString()}).ToList(); ViewBag.categoryId = id; return View(product); } }
public ActionResult Create(int categoryId, FormCollection form) { try { using (var context = new SiteContainer()) { var category = context.Category.First(c => c.Id == categoryId); var product = new Product { Category = category, ImageSource = "" }; TryUpdateModel(product, new[] { "Title", "Discount", "Price", "Structure", "Consistence", "Producer", "Nap", "PageTitle" }); product.Description = HttpUtility.HtmlDecode(form["Description"]); product.DiscountText = HttpUtility.HtmlDecode(form["DiscountText"]); 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); GraphicsHelper.SaveOriginalImage(filePath, fileName, file, 1500); //file.SaveAs(filePath); pi.ImageSource = fileName; product.ProductImages.Add(pi); if (string.IsNullOrEmpty(product.ImageSource)) product.ImageSource = pi.ImageSource; } context.AddToProduct(product); context.SaveChanges(); if (category.SpecialCategory) { return RedirectToAction("OurWorks", "Home", new { area = "" }); } return RedirectToAction("Gallery", "Home", new { area = "", id = category.Name }); } } catch (Exception ex) { return View(ViewBag.ErrorMessage = ex.Message); } }
public ActionResult Create(int categoryId, FormCollection form, HttpPostedFileBase fileUpload) { try { using (var context = new SiteContainer()) { var category = context.Category.First(c => c.Id == categoryId); var product = new Product{Category = category}; TryUpdateModel(product, new[] {"Title", "Discount", "DiscountText", "Price"}); product.Description = HttpUtility.HtmlDecode(form["Description"]); if (fileUpload != null) { //if (!string.IsNullOrEmpty(product.ImageSource)) //{ // IOHelper.DeleteFile("~/Content/Images", product.ImageSource); // foreach (var thumbnail in SiteSettings.Thumbnails) // { // IOHelper.DeleteFile("~/ImageCache/" + thumbnail.Key, product.ImageSource); // } //} string fileName = IOHelper.GetUniqueFileName("~/Content/Images", fileUpload.FileName); string filePath = Server.MapPath("~/Content/Images"); filePath = Path.Combine(filePath, fileName); fileUpload.SaveAs(filePath); product.ImageSource = fileName; } context.AddToProduct(product); context.SaveChanges(); } return RedirectToAction("Index"); } catch { return View(); } }
public SiteModel(SiteContainer context, string contentName, bool showArticles = false, bool showOurWorks = false) { Title = "Килими"; if (contentName == null) { Content = context.Content.First(c => c.MainPage); } else { Content = context.Content.FirstOrDefault(c => c.Name == contentName); if (Content == null) { throw new HttpNotFoundException(); } } if (!string.IsNullOrEmpty(Content.Title)) Title += " » " + Content.Title; PageTitle = Content.PageTitle; SeoDescription = Content.SeoDescription; SeoKeywords = Content.SeoKeywords; if (Content.MainPage) { IsHomePage = true; } if (showArticles) { Articles = context.Article.OrderByDescending(a => a.Date).ToList(); } if (showOurWorks) { OurWorks = context.Category.Include("Products").First(c => c.Name == "ourworks"); } if (IsHomePage) { MainPageCategories = new List<Category>(); var categories = new List<CategoryProductPresentation>(); var conn = DbHelper.Connection; using (conn.StateManager()) { var query = @"select parent.Id as CategoryId, parent.Title as CategoryTitle, parent.Name as CategoryName, t2.title as ProductTitle, t2.DiscountText as DiscountText, t2.Id as ProductId, t2.ImageSource as ImageSource from Category t1 join Product t2 on t2.CategoryId=t1.Id join Category parent on parent.Id=t1.CategoryId";// where parent.ShowOnMainPage=1"; conn.ReadToCollection(categories, r => CategoryProductPresentation.Init(new CategoryProductPresentation(), r), query); int oldCategoryId = 0; int newCategoryId; Category category = null; foreach (var presentation in categories.OrderBy(c => c.CategoryId)) { newCategoryId = presentation.CategoryId; if (oldCategoryId != newCategoryId) { if (category != null) MainPageCategories.Add(category); category = new Category { Id = presentation.CategoryId, Title = presentation.CategoryTitle, Name = presentation.CategoryName }; } var p = new Product { Id = presentation.ProductId, Title = presentation.ProductTitle, ImageSource = presentation.ProductImageSource, DiscountText = presentation.ProductDiscountText }; category.Products.Add(p); oldCategoryId = newCategoryId; } if (category != null) MainPageCategories.Add(category); } //var allCategories = context.Category.Include("Children").Include("Products").Where(c => c.ShowOnMainPage).ToList(); //foreach (var category in allCategories.Where(c => c.Parent == null && !c.SpecialCategory)) //{ // var cat = new Category { Name = category.Name, Title = category.Title, Id = category.Id }; // foreach (var child in category.Children) // { // foreach (var product in child.Products) // { // var p = new Product { Id = product.Id, ImageSource = product.ImageSource, Title = product.Title, Description = product.Description, Discount = product.Discount, DiscountText = product.DiscountText }; // cat.Products.Add(p); // } // } // MainPageCategories.Add(cat); //} Banners = context.Banner.ToList(); } }
/// <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="discount">Initial value of the Discount 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.Boolean discount, global::System.Int32 categoryId) { Product product = new Product(); product.Id = id; product.Title = title; product.ImageSource = imageSource; product.Discount = discount; product.CategoryId = categoryId; return product; }
/// <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); }
public GalleryModel(SiteContainer context, string categoryId, int? productId = null, int? productImageId = null) : base(context, "gallery", false) { _context = context; if (productId.HasValue) { Product = _context.Product.Include("Category").Include("ProductSizes").Include("ProductImages").First(p => p.Id == productId); if (productImageId.HasValue) { foreach (var productImage in Product.ProductImages.Where(productImage => productImage.Id == productImageId)) { productImage.Selected = true; } } if (categoryId == null) { categoryId = Product.Category.Name; } PageTitle = Product.PageTitle; } //if (productId.HasValue) //{ // var product = context.Product.Include("Category").First(p => p.Id == productId); // categoryId = product.Category.Name; //} categoryId = GetFirstCategory(categoryId); if (!string.IsNullOrEmpty(categoryId)) { Category = _context.Category.Include("Parent") .Include("Products") //.Include("ProductSizes") .First(c => c.Name == categoryId); ParentCategory = Category.Parent ?? Category; } Categories = _context.Category.Include("Children").Where(c => c.Parent == null&& !c.SpecialCategory).ToList(); foreach (var category in Categories) { if (category.Name == categoryId) { category.Selected = true; Title += " - " + category.Title; SeoDescription = category.SeoDescription; SeoKeywords = category.SeoKeywords; PageTitle = category.PageTitle; } foreach (var child in category.Children.Where(child => child.Name == categoryId)) { child.Selected = true; Title += " - " + category.Title + " - " + child.Title; SeoDescription = child.SeoDescription; SeoKeywords = child.SeoKeywords; category.IsParent = true; PageTitle = child.PageTitle; } } }