public ActionResult Edit(Customer customer)
        {
            if (ModelState.IsValid)
            {
                db.Entry(customer).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            //get all countries and put them into list of select list items
            var dropDownItems = GetAllCountries();
            // sort list into alphabetical order
            var items = from i in dropDownItems orderby i.Text == customer.visitingCountry, i.Text ascending select i;
            //transfer country data into view using viewbag 
            ViewBag.countries = items;

            ViewBag.customerGroupCode = new SelectList(db.CustomerGroups, "customerGroupCode", "customerGroupName",
                customer.customerGroupCode);
            ViewBag.customerStatus = new SelectList(db.CustomerStatus, "customerStatusNo", "customerStatusText",
                customer.customerStatus);
            ViewBag.termsOfDelivery = new SelectList(db.TermsOfDeliveries, "todNo", "todText", customer.termsOfDelivery);
            ViewBag.termsOfPayment = new SelectList(db.TermsOfPayments, "topNo", "topText", customer.termsOfPayment);
            return View(customer);
        }
        //
        // GET: /Customer/Create

        public ActionResult Create(int id = 0)
        {
            if (id == 0)
            {
                ViewBag.customerGroupCode = new SelectList(db.CustomerGroups, "customerGroupCode", "customerGroupName");
                ViewBag.customerStatus = new SelectList(db.CustomerStatus, "customerStatusNo", "customerStatusText");
                ViewBag.termsOfDelivery = new SelectList(db.TermsOfDeliveries, "todNo", "todText");
                ViewBag.termsOfPayment = new SelectList(db.TermsOfPayments, "topNo", "topText");
                List<SelectListItem> dropDownItems;

                //get all countries and put them into list of select list items
                dropDownItems = GetAllCountries();
                // sort list into alphabetical order
                var items = from i in dropDownItems orderby i.Text select i;
                //transfer country data into view using viewbag 
                ViewBag.countries = items;

                return View();
            }

            List<SelectListItem> dropDownItems2;

            
            var site = db.CustomerSites.Find(id);
            Customer customer = new Customer();

            customer.postalStreet = site.postalStreet;
            customer.postalPostal = site.postalPostal;
            customer.postalCity = site.postalCity;
            customer.postalCountry = site.postalCountry;

            customer.visitingStreet = site.visitingStreet;
            customer.visitingCity = site.visitingCity;
            customer.visitingPostal = site.visitingPostal;
            customer.visitingCountry = site.visitingCountry;

            customer.contactAddressStreet = site.contactAddressStreet;
            customer.contactAddressCountry = site.contactAddressCountry;
            customer.contactAddressPostal = site.contactAddressPostal;
            customer.contactAddressCity = site.contactAddressCity;

            customer.contactEmail = site.contactEmail;
            customer.contactName = site.contactName;
            customer.contactPhone = site.contactPhone;
            customer.contactTitle = site.contactTitle;

            ViewBag.customerGroupCode = new SelectList(db.CustomerGroups, "customerGroupCode", "customerGroupName");
            ViewBag.customerStatus = new SelectList(db.CustomerStatus, "customerStatusNo", "customerStatusText");
            ViewBag.termsOfDelivery = new SelectList(db.TermsOfDeliveries, "todNo", "todText");
            ViewBag.termsOfPayment = new SelectList(db.TermsOfPayments, "topNo", "topText");

            //get all countries and put them into list of select list items
            dropDownItems2 = GetAllCountries();
            // sort list into alphabetical order
            var items2 = from i in dropDownItems2 orderby i.Text select i;
            //transfer country data into view using viewbag 
            ViewBag.countries = items2;

            TempData["siteNumber"] = site;

            return View(customer);
        }
        public ActionResult Create(Customer customer)
        {
            // Remove customerNo from modelstate so it allows customerNo to be added last.         
            // Problem was that the modelstate was still expecting the customerNo to be added at the create new customer page
            ModelState.Remove("customerNo");
            if (customer.visitingStreet == null)
            {
                customer.visitingStreet = customer.postalStreet;
            }
            if (customer.visitingPostal == null)
            {
                customer.visitingPostal = customer.postalPostal;
            }
            if (customer.visitingCity == null)
            {
                customer.visitingCity = customer.postalCity;
            }
            if (customer.visitingCountry == null)
            {
                customer.visitingCountry = customer.postalCountry;
            }

            if (ModelState.IsValid)
            {
                
                // generate new customer id
                var lastId = db.Customers.Max(item => item.customerNo);
                var newId = lastId + 1;
                customer.customerNo = newId;

                CustomerSite site = new CustomerSite();
                site = TempData["siteNumber"] as CustomerSite;

                if (site != null)
                {
                    site.customerNo = customer.customerNo;
                    db.Customers.Add(customer);
                    db.SaveChanges();

                    db.Entry(site).State = EntityState.Modified;
                    db.SaveChanges();

                    return RedirectToAction("Index");
                }

                db.Customers.Add(customer);
                db.SaveChanges();
              
                return RedirectToAction("Index");
            }

            ViewBag.customerGroupCode = new SelectList(db.CustomerGroups, "customerGroupCode", "customerGroupName",
                customer.customerGroupCode);
            ViewBag.customerStatus = new SelectList(db.CustomerStatus, "customerStatusNo", "customerStatusText",
                customer.customerStatus);
            ViewBag.termsOfDelivery = new SelectList(db.TermsOfDeliveries, "todNo", "todText", customer.termsOfDelivery);
            ViewBag.termsOfPayment = new SelectList(db.TermsOfPayments, "topNo", "topText", customer.termsOfPayment);

            //get all countries and put them into list of select list items
            var dropDownItems2 = GetAllCountries();
            // sort list into alphabetical order
            var items2 = from i in dropDownItems2 orderby i.Text select i;
            //transfer country data into view using viewbag 
            ViewBag.countries = items2;
            return View(customer);
        }