public AddProductsWindow(Order order) { InitializeComponent(); //Binding view with ViewModel DataContext = new AddProductsViewModel(this, order); }
public ActionResult AddProduct() { AddProductsViewModel apvm = new AddProductsViewModel(); apvm.Categories = dbContext.Categories.ToList(); apvm.Product = new Product(); return(View(apvm)); }
public ActionResult EditProduct(int?id) { if (id == null) { return(RedirectToAction("Products", "Admin")); } AddProductsViewModel apvm = new AddProductsViewModel(); apvm.Product = new Product(); apvm.Product = dbContext.Products.Where(p => p.Id == id).FirstOrDefault(); apvm.Categories = dbContext.Categories.ToList(); return(View(apvm)); }
public async Task <IActionResult> AddProduct() { if (!LoggedIn()) { return(RedirectToAction("Login")); } List <Brand> brands = await _db.Brands.OrderBy(el => el.Name).ToListAsync(); List <Category> categories = await _db.Categories.OrderBy(el => el.Name).ToListAsync(); List <Gender> genders = await _db.Genders.OrderBy(el => el.Name).ToListAsync(); AddProductsViewModel model = new AddProductsViewModel { Brands = brands, Categories = categories, Genders = genders }; return(View(model)); }
public ActionResult Edit(int id) { var product = db.ProductsRepository.GetById(id); if (product == null) { return(HttpNotFound()); } var editProduct = new AddProductsViewModel { ProductId = id, ProductTitle = product.ProductTitle, Description = product.Description, ShortDescription = product.ShortDescription, Price = product.Price, Tags = string.Join(".", product.ProductTags.Select(t => t.Tag)) }; ViewBag.ImageName = product.ImageName; ViewBag.ProductGroups = db.ProductGroupsRepository.GetAll(); return(View(editProduct)); }
public ActionResult AddProduct([Bind(Include = "title,desc,count,price")] Product prod, HttpPostedFileBase upload, string CategoryTitle) { AddProductsViewModel apvm = new AddProductsViewModel(); prod.CategoryId = dbContext.Categories.Where(c => c.title == CategoryTitle).FirstOrDefault().Id; apvm.Product = prod; apvm.Categories = dbContext.Categories.ToList(); if (ModelState.IsValidField("title") == false) { ModelState.AddModelError("Product.title", "Поле не заполненно"); } if (ModelState.IsValidField("count") == false) { ModelState.AddModelError("Product.count", "Поле не заполненно либо заполненно не верно"); } if (ModelState.IsValidField("price") == false) { ModelState.AddModelError("Product.price", "Поле не заполненно либо заполненно не верно"); } if (ModelState.IsValid) { if (upload != null) { // получаем имя файла string fileName = System.IO.Path.GetFileName(upload.FileName); // сохраняем файл в папку Files в проекте upload.SaveAs(Server.MapPath("~/Images/" + fileName)); prod.img = "/Images/" + fileName; } dbContext.Products.Add(prod); dbContext.SaveChanges(); return(RedirectToAction("Products", "Admin")); } return(View(apvm)); }
public AddProductView() { InitializeComponent(); BindingContext = new AddProductsViewModel(); }
public async Task <IActionResult> AddProduct(AddProductsViewModel model) { if (!LoggedIn()) { return(RedirectToAction("Login")); } if (string.IsNullOrWhiteSpace(model.Product.Name)) { ModelState.AddModelError("", "Name is null or white space"); } if (model.Product.Price < 0) { ModelState.AddModelError("", "Price must be greater than 0"); } List <string> Sizes = new List <string>(); if (model.Sizes != null) { foreach (var item in model.Sizes) { if (!string.IsNullOrWhiteSpace(item)) { Sizes.Add(item); } } } List <IFormFile> ProductImgs = new List <IFormFile>(); if (model.ProductImgs == null || model.ProductImgs.Count == 0) { ModelState.AddModelError("", "Product imgs is null or empty"); } else { foreach (var item in model.ProductImgs) { if (item.FileName.Contains(".jpg") || item.FileName.Contains(".jpeg") || item.FileName.Contains(".png")) { ProductImgs.Add(item); } } if (ProductImgs.Count() == 0) { ModelState.AddModelError("", "Product imgs is empty"); } } if (model.SelectedBrand.Id == -1 && string.IsNullOrWhiteSpace(model.NewBrand.Name)) { ModelState.AddModelError("", "You have not entered a brand name"); if (model.BrandImg != null && !(model.BrandImg.FileName.Contains(".jpg") || model.BrandImg.FileName.Contains(".jpeg") || model.BrandImg.FileName.Contains(".png"))) { ModelState.AddModelError("", "Brand img isn`t valid"); } } if (model.SelectedCategory.Id == -1 && string.IsNullOrWhiteSpace(model.NewCategory.Name)) { ModelState.AddModelError("", "You have not entered a category name"); } if (model.SelectedGender.Id == -1 && string.IsNullOrWhiteSpace(model.NewGender.Name)) { ModelState.AddModelError("", "You have not entered a gender name"); } if (!ModelState.IsValid) { List <Brand> brands = await _db.Brands.OrderBy(el => el.Name).ToListAsync(); List <Category> categories = await _db.Categories.OrderBy(el => el.Name).ToListAsync(); List <Gender> genders = await _db.Genders.OrderBy(el => el.Name).ToListAsync(); model.Brands = brands; model.Categories = categories; model.Genders = genders; return(View(model)); } Brand brand; if (model.SelectedBrand.Id == -1) { brand = new Brand(); brand.Name = model.NewBrand.Name; if (model.BrandImg != null) { string path = "/img/Brands/" + model.BrandImg.FileName; using (var fileStream = new FileStream(_appEnvironment.WebRootPath + path, FileMode.Create)) { model.BrandImg.CopyTo(fileStream); } brand.ImgUrl = "img/Brands/" + model.BrandImg.FileName; _db.Brands.Add(brand); _db.SaveChanges(); brand = _db.Brands.FirstOrDefault(el => el.Name.Equals(brand.Name)); } } else { brand = _db.Brands.FirstOrDefault(el => el.Id == model.SelectedBrand.Id); } Category category; if (model.SelectedCategory.Id == -1) { category = new Category(); category.Name = model.NewCategory.Name; _db.Categories.Add(category); _db.SaveChanges(); category = _db.Categories.FirstOrDefault(el => el.Name.Equals(category.Name)); } else { category = _db.Categories.FirstOrDefault(el => el.Id == model.SelectedCategory.Id); } Gender gender; if (model.SelectedGender.Id == -1) { gender = new Gender(); gender.Name = model.NewGender.Name; _db.Genders.Add(gender); _db.SaveChanges(); gender = _db.Genders.FirstOrDefault(el => el.Name.Equals(gender.Name)); } else { gender = _db.Genders.FirstOrDefault(el => el.Id == model.SelectedGender.Id); } Product product = new Product(); product.Name = model.Product.Name; product.Price = model.Product.Price; string[] ImgUrls = new string[ProductImgs.Count()]; for (int i = 0; i < ProductImgs.Count(); i++) { string path = "/img/Products/" + ProductImgs[i].FileName; using (var fileStream = new FileStream(_appEnvironment.WebRootPath + path, FileMode.Create)) { ProductImgs[i].CopyTo(fileStream); } ImgUrls[i] = "img/Products/" + ProductImgs[i].FileName; } product.ImgUrls = JsonSerializer.Serialize <string[]>(ImgUrls); product.IsPopular = model.Product.IsPopular; product.Sizes = JsonSerializer.Serialize <string[]>(Sizes.ToArray()); if (!string.IsNullOrWhiteSpace(model.Product.Country)) { product.Country = model.Product.Country; } if (!string.IsNullOrWhiteSpace(model.Product.Composition)) { product.Composition = model.Product.Composition; } if (!string.IsNullOrWhiteSpace(model.Product.Description)) { product.Description = model.Product.Description; } product.Brand = brand; product.BrandId = brand.Id; product.Gender = gender; product.GenderId = gender.Id; product.Category = category; product.CategoryId = category.Id; _db.Products.Add(product); _db.SaveChanges(); return(RedirectToAction("ProductsPage")); }
public ActionResult Create([Bind(Include = "ProductTitle,ShortDescription,Description,Price,Tags")] AddProductsViewModel model, HttpPostedFileBase imageProduct, List <int> selectedPg) { if (ModelState.IsValid) { if (selectedPg == null) { ViewBag.ProductGroups = db.ProductGroupsRepository.GetAll(); ViewBag.ErrorPG = true; return(View(model)); } var product = new Products { ProductTitle = model.ProductTitle, ShortDescription = model.ShortDescription, Description = model.Description, Price = model.Price, ProductRate = 0, CreateDate = DateTime.Now }; //Insert Tags if (string.IsNullOrEmpty(model.Tags) == false) { var tags = model.Tags.Split('.'); foreach (var tag in tags) { if (string.IsNullOrEmpty(tag)) { ViewBag.ProductGroups = db.ProductGroupsRepository.GetAll(); ModelState.AddModelError("Tags", "لطفا کلمات کلیدی را به درستی با نقطه(.) جدا کنید."); return(View(model)); } db.ProductTagsRepository.Insert(new ProductTags { ProductId = product.ProductId, Tag = tag.Trim() }); } } product.ImageName = "Default.png"; if (imageProduct != null && imageProduct.IsImage()) { product.ImageName = Guid.NewGuid() + Path.GetExtension(imageProduct.FileName); } db.ProductsRepository.Insert(product); foreach (var item in selectedPg) { db.SelectedProductGroupRepository.Insert(new SelectedProductGroup() { GroupId = item, ProductId = product.ProductId }); } db.Save(); if (product.ImageName != "Default.png") { imageProduct.SaveAs(Server.MapPath("/Images/ProductImages/" + product.ImageName)); var img = new ImageResizer(); img.Resize(Server.MapPath("/Images/ProductImages/" + product.ImageName), Server.MapPath("/Images/ProductImages/Resized/" + product.ImageName)); } return(RedirectToAction("Index")); } ViewBag.ProductGroups = db.ProductGroupsRepository.GetAll(); return(View(model)); }
public ActionResult Edit([Bind(Include = "ProductTitle,ShortDescription,Description,Price,Tags,ProductId")] AddProductsViewModel model, HttpPostedFileBase imageProduct, List <int> selectedPg, int id) { if (ModelState.IsValid) { if (selectedPg == null) { ViewBag.ProductGroups = db.ProductGroupsRepository.GetAll(); ViewBag.ErrorPG = true; return(View(model)); } //Product info Edit var product = db.ProductsRepository.GetById(model.ProductId); product.ProductTitle = model.ProductTitle; product.Description = model.Description; product.ShortDescription = model.ShortDescription; product.Price = model.Price; //Image Edit if (imageProduct != null && imageProduct.IsImage()) { if (product.ImageName != "Default.png") { System.IO.File.Delete(Server.MapPath("/Images/ProductImages/" + product.ImageName)); System.IO.File.Delete(Server.MapPath("/Images/ProductImages/Resized/" + product.ImageName)); } var imageName = Guid.NewGuid() + Path.GetExtension(imageProduct.FileName); imageProduct.SaveAs(Server.MapPath("/Images/ProductImages/" + imageName)); var img = new ImageResizer(); img.Resize(Server.MapPath("/Images/ProductImages/" + imageName), Server.MapPath("/Images/ProductImages/Resized/" + imageName)); product.ImageName = imageName; } //Tags Edit var strTag = string.Join(".", product.ProductTags.Select(t => t.Tag)); if (string.IsNullOrEmpty(model.Tags) == false && strTag != model.Tags) { foreach (var tag in product.ProductTags.ToList()) { db.ProductTagsRepository.Delete(tag); } var newTags = model.Tags.Split('.'); foreach (var tag in newTags) { db.ProductTagsRepository.Insert(new ProductTags { ProductId = product.ProductId, Tag = tag.Trim() }); } } //Delete The Groups var selectedGroups = product.SelectedProductGroups.ToList(); foreach (var group in selectedGroups) { foreach (var item in selectedPg) { if (item == group.GroupId) { goto loop; } } db.SelectedProductGroupRepository.Delete(group); loop :; } //Insert The Groups foreach (var item in selectedPg) { foreach (var group in selectedGroups) { if (item == group.GroupId) { goto loop; } } db.SelectedProductGroupRepository.Insert(new SelectedProductGroup() { GroupId = item, ProductId = product.ProductId }); loop :; } db.Save(); return(RedirectToAction("Index")); } ViewBag.ProductGroups = db.ProductGroupsRepository.GetAll(); return(View(model)); }