Exemple #1
0
        public ActionResult EditCustomer(Customer model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    Customer customer = Uow.Customers.GetById(model.Id);
                    if (customer != null)
                    {
                        customer.CustomerCode = model.CustomerCode;
                        customer.CustomerName = model.CustomerName;
                        customer.Issuer = model.Issuer;
                        customer.LicenseId = model.LicenseId;
                        customer.SecretKey = model.SecretKey;
                        customer.ServiceBusNamespace = model.ServiceBusNamespace;

                        Uow.Customers.Update(customer);
                        Uow.Commit();
                    }

                    return RedirectToAction("Index", "Customer");
                }
                catch (DbUpdateException ex)
                {
                    string errorMessage = ex.GetInnerMostException().Message;
                    if (errorMessage.Contains("duplicate key") && errorMessage.Contains("LicenseId"))
                        ModelState.AddModelError(typeof(DbUpdateException).Name, string.Format("LicenseId {0} already taken", model.LicenseId));
                    else if (errorMessage.Contains("duplicate key") && errorMessage.Contains("CustomerCode"))
                        ModelState.AddModelError(typeof(DbUpdateException).Name, string.Format("Customer Code {0} already taken", model.CustomerCode));
                    else
                        ModelState.AddModelError(typeof(DbUpdateException).Name, errorMessage);
                }
                catch (Exception e)
                {
                    ModelState.AddModelError("", e);
                }
            }

            return View(model);
        }
Exemple #2
0
        public ActionResult NewCustomer(Customer model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    Uow.Customers.Add(model);
                    Uow.Commit();
                    return RedirectToAction("Index", "Customer");
                }
                catch (DbUpdateException ex)
                {
                    string errorMessage = ex.GetInnerMostException().Message;
                    if(errorMessage.Contains("duplicate key") && errorMessage.Contains("LicenseId"))
                        ModelState.AddModelError(typeof(DbUpdateException).Name, string.Format("License Id {0} already taken", model.LicenseId));
                    else if(errorMessage.Contains("duplicate key") && errorMessage.Contains("CustomerCode"))
                        ModelState.AddModelError(typeof(DbUpdateException).Name, string.Format("Customer Code {0} already taken", model.CustomerCode));
                    else
                        ModelState.AddModelError(typeof(DbUpdateException).Name, errorMessage);
                }
                catch (Exception e)
                {
                    ModelState.AddModelError("", e);
                }
            }

            return View(model);
        }