Пример #1
0
        public ActionResult CreateForProductLocation(SerialNumber serialNumber)
        {
            serialNumber.Used         = false;
            serialNumber.DateInserted = DateTime.Now;

            int ProductInGameID = db.ProductLocations.Find(serialNumber.ProductLocationID).ProductInGameID;

            SerialNumberValidator validator = new SerialNumberValidator();

            if (ModelState.IsValid && validator.Validate(serialNumber).IsValid)
            {
                db.SerialNumbers.Add(serialNumber);
                db.SaveChanges();
                return(RedirectToAction("EditFromProductInGame", "ProductLocations", new { id = serialNumber.ProductLocationID, productInGameID = ProductInGameID }));
            }


            FluentValidation.Results.ValidationResult results = validator.Validate(serialNumber);
            IList <ValidationFailure> failures = results.Errors;
            StringBuilder             sb       = new StringBuilder();

            foreach (var e in results.Errors)
            {
                ModelState.AddModelError(e.PropertyName + "Error", e.ErrorMessage);
                sb.AppendLine(e.ErrorMessage);
            }

            ViewBag.ProductLocationID = serialNumber.ProductLocationID;
            return(View(serialNumber));
        }
Пример #2
0
        public ActionResult EditFromProductInGame(ProductLocationViewModel plvm)
        {
            StringBuilder sb = new StringBuilder();

            if (ModelState.IsValid)
            {
                db.Entry(plvm.ProductLocation).State = EntityState.Modified;

                foreach (var s in plvm.ProductLocation.SerialNumbers)
                {
                    SerialNumberValidator validator = new SerialNumberValidator();

                    FluentValidation.Results.ValidationResult results = validator.Validate(s);
                    IList <ValidationFailure> failures = results.Errors;

                    foreach (var e in results.Errors)
                    {
                        ModelState.AddModelError(e.PropertyName + "Error", e.ErrorMessage);
                        sb.AppendLine(e.ErrorMessage);
                    }


                    db.Entry(s).State = EntityState.Modified;
                }
                foreach (var v in plvm.ProductLocation.Vouchers)
                {
                    db.Entry(v).State = EntityState.Modified;
                }

                if (ModelState.IsValid)
                {
                    db.SaveChanges();
                    return(RedirectToAction("Edit", "ProductInGames", new { id = plvm.ProductLocation.ProductInGameID }));
                }
                else
                {
                    ViewBag.ProductInGameID            = plvm.ProductLocation.ProductInGameID;
                    ViewBag.ErrorMessage               = sb.ToString();
                    plvm.ProductLocation.ProductInGame = db.ProductInGames.Find(plvm.ProductLocation.ProductInGameID);
                    return(View(plvm));
                }
            }
            ViewBag.ErrorMessage               = sb.ToString();
            ViewBag.ProductInGameID            = plvm.ProductLocation.ProductInGameID;
            plvm.ProductLocation.ProductInGame = db.ProductInGames.Find(plvm.ProductLocation.ProductInGameID);
            return(View(plvm));
        }