Esempio n. 1
0
 public static CustomerModel CustomerEntityToModel(Customer customer)
 {
     CustomerModel customerModel = new CustomerModel();
     customerModel.CustomerID = customer.CustomerID;
     customerModel.CompanyName = customer.CompanyName;
     customerModel.ContactName = customer.ContactName;
     customerModel.ContactTitle = customer.ContactTitle;
     customerModel.Address = customer.Address;
     customerModel.City = customer.City;
     customerModel.PostalCode = customer.PostalCode;
     customerModel.Region = customer.Region;
     customerModel.Country = customer.Country;
     customerModel.Phone = customer.Phone;
     customerModel.Fax = customer.Fax;
     return customerModel;
 }
Esempio n. 2
0
        public static Customer UpdateCustomerModelToEntity(Customer customer, CustomerModel cusModel)
        {
            //Customer customer = new Customer();
            customer.CustomerID = cusModel.CustomerID;
            customer.CompanyName = cusModel.CompanyName;
            customer.ContactName = cusModel.ContactName;
            customer.ContactTitle = cusModel.ContactTitle;
            customer.Address = cusModel.Address;
            customer.City = cusModel.City;
            customer.PostalCode = cusModel.PostalCode;
            customer.Region = cusModel.Region;
            customer.Country = cusModel.Country;
            customer.Phone = cusModel.Phone;
            customer.Fax = cusModel.Fax;

            return customer;
        }
        public ActionResult Create(CustomerModel cusModel)
        {
            try
            {
                using (var dbContext = new NORTHWNDContext())
                {
                    Customer customer = Mapper.CustomerModelToEntity(cusModel);
                    dbContext.Customers.Add(customer);
                    dbContext.SaveChanges();

                }

            }
            catch (Exception ex)
            {

                throw new ArgumentException(ex.Message);
            }
            return RedirectToAction("Index", "Default");
        }
        public ActionResult Edit(CustomerModel cusModel)
        {
            try
            {

                using (var dbContext = new NORTHWNDContext())
                {
                    Customer customer = dbContext.Customers.Where(o => o.CustomerID == cusModel.CustomerID).First();
                    if (customer == null)
                    {
                        ViewBag.ErrorMessage = "User was not found";
                    }
                    else
                    {
                        customer = Mapper.UpdateCustomerModelToEntity(customer, cusModel);
                        dbContext.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {

                throw new ArgumentException(ex.Message);
            }
            return RedirectToAction("Index", "Default");
        }