Пример #1
0
        public ActionResult Index()
        {
            var model = new PIMModel { Brands = GetBrands() };

            return View(model);
        }
Пример #2
0
        public ActionResult Index(PIMModel model, HttpPostedFileBase file)
        {
            model.Brands = GetBrands();

            if (ModelState.IsValid)
            {
                int added = 0;
                int updated = 0;

                if (file != null && file.ContentLength > 0 && file.FileName.Contains("xls"))
                {
                    var fileName = Path.GetFileName(file.FileName);
                    var path = Path.Combine(Server.MapPath("~/Files/upload"), fileName);
                    file.SaveAs(path);
                    var dt = ReadSpreadsheetIntoDataTable(path, "ProductInfo");
                    System.IO.File.Delete(path);

                    using (var db = new PIMContext())
                    {
                        foreach (DataRow row in dt.Rows)
                        {
                            if (string.IsNullOrEmpty(row["Style Number"].ToString().Trim()))
                            {
                                ModelState.AddModelError("StyleError", "A style number is missing. Please correct and re-upload.");
                                return View(model);
                            }
                            string patternName = row["Pattern Name"].ToString().Trim();
                            var pattern = db.Patterns.SingleOrDefault(p => p.PatternName == patternName);
                            if (pattern == null) pattern = new Pattern();
                            pattern.BrandId = model.BrandId;
                            pattern.PatternName = patternName;
                            pattern.LastUpdated = DateTime.Now;
                            // Add if it doesn't exist otherwise just updates the existing pattern that was loaded
                            db.Entry(pattern).State = pattern.PatternId == 0 ? EntityState.Added : EntityState.Modified;
                            db.SaveChanges(); // Need to save the pattern first to get an id

                            string styleNumber = row["Style Number"].ToString().Trim();
                            var style = db.Styles.SingleOrDefault(s => s.StockNumber == styleNumber);
                            if (style == null) style = new Style();
                            style.PatternId = pattern.PatternId;
                            style.StockNumber = styleNumber;
                            style.MarketingDescription = row["Marketing Description"].ToString();
                            style.TechBullets = !string.IsNullOrEmpty(row["Tech Bullets"].ToString()) ?
                                row["Tech Bullets"].ToString() : null;
                            style.LastUpdated = DateTime.Now;
                            style.LastUpdatedBy = User.Identity.Name;

                            db.Entry(style).State = style.StyleId == 0 ? EntityState.Added : EntityState.Modified;
                            if (style.StyleId == 0)
                                added++;
                            else
                                updated++;
                            db.SaveChanges();
                        }
                    }
                }
                else
                {
                    ModelState.AddModelError("XlsError", "Please upload a valid Excel file.");
                    return View(model);
                }

                return RedirectToAction("ViewProducts", new { b = model.BrandId, a = added, u = updated });
            }

            return View(model);
        }
Пример #3
0
        public ActionResult ViewProducts(int a = 0, int u = 0)
        {
            var model = new PIMModel { Brands = GetBrands() };
            ViewBag.Added = a;
            ViewBag.Updated = u;

            return View(model);
        }