コード例 #1
0
        public ViewResult AddUpdateSupplier(AddUpdateSupplierModel model)
        {
            string   errorMessage;
            Supplier supplierToUpdate = null;

            try
            {
                if (model.IsValid(out errorMessage))
                {
                    // If we're updating a supplier
                    if (model.SupplierID.HasValue)
                    {
                        using (ProductListContext productContext = new ProductListContext())
                        {
                            supplierToUpdate = productContext.Suppliers.FirstOrDefault(s => s.SupplierID == model.SupplierID);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                errorMessage = Errors.GenericMVCInternalError;
                ErrorLog.LogError(e);
            }

            ViewResult viewResult;

            if (errorMessage != null)
            {
                // Error, show the message
                ProductListViewModel viewModel;
                using (ProductListContext productContext = new ProductListContext())
                {
                    viewModel = ProductListViewModel.GetModel(productContext, errorMessage: errorMessage);
                }

                viewResult = View("Index", viewModel);
            }
            else
            {
                // Load the Add/Update view
                AddUpdateSupplierViewModel viewModel = null;

                viewModel  = AddUpdateSupplierViewModel.GetModel(supplierToUpdate);
                viewResult = View(viewModel);
            }

            return(viewResult);
        }
コード例 #2
0
        public ActionResult UpdateSupplier(UpdateSupplierModel model)
        {
            bool     updatedSupplier;
            string   errorMessage;
            Supplier formSupplierData = null;

            try
            {
                if (model.IsValid(out errorMessage))
                {
                    using (ProductListContext productContext = new ProductListContext())
                    {
                        // update the supplier
                        productContext.UpdateSupplier(model.SupplierID,
                                                      model.Name,
                                                      model.PhoneNumber);

                        productContext.SaveChanges();
                    }

                    updatedSupplier = true;
                }
                else
                {
                    // save form data for when we return
                    formSupplierData = new Supplier
                    {
                        SupplierID  = model.SupplierID,
                        Name        = model.Name,
                        PhoneNumber = model.PhoneNumber
                    };

                    updatedSupplier = false;
                }
            }
            catch (Exception e)
            {
                updatedSupplier = false;
                errorMessage    = Errors.GenericMVCInternalError;
                ErrorLog.LogError(e);
            }

            ActionResult result;

            if (updatedSupplier)
            {
                // Updated product, return to list
                ProductListModel viewModel = new ProductListModel
                {
                    AddedOrUpdatedProduct = true
                };

                result = RedirectToAction("Index", viewModel);
            }
            else
            {
                // Error, show message
                AddUpdateSupplierViewModel viewModel = AddUpdateSupplierViewModel.GetModel(formSupplierData, errorMessage);
                result = View("AddUpdateSupplier", viewModel);
            }

            return(result);
        }