Example #1
0
        public IActionResult CreateProductType(ProductTypeAddModel addModel)
        {
            //check validation
            if (ModelState.IsValid)
            {
                //add product type to database
                eCommerce.AddProductType(addModel, out ICollection <string> errors);
                //return if error happen
                if (errors.Any())
                {
                    ViewData[GlobalViewBagKeys.Errors] = errors;
                    return(View(addModel));
                }

                //get the just added product type
                try
                {
                    //find exactly 1 record
                    ProductTypeView productType = eCommerce
                                                  .GetProductTypesBy(new ProductTypeSearchModel
                    {
                        DateTimeModified = addModel.DateModified,
                        SearchString     = addModel.Name
                    }, null, null)
                                                  .Single();

                    //if found exactly 1 record then continue to next step
                    return(RedirectToAction("Index", new { productTypeId = int.Parse(productType.Id) }));
                }
                catch (Exception)               //if not exactly 1 record then throw error and return
                {
                    errors.Add("There is a problem adding product type please try again");
                }
            }
            return(View(addModel));
        }