// GET: CustomerRegions
        //public async Task<IActionResult> Index()
        public IActionResult Index()
        {
            List <CustomerCountryRegionVM> countryRegionList = new List <CustomerCountryRegionVM>();

            try
            {
                var regionsWithTheirCountry = (from r in _context.CustomerRegions
                                               join c in _context.CustomerCountries
                                               on r.IdCustomerCountry equals c.IdCustomerCountry
                                               select new
                {
                    r.IdCustomerRegion,
                    r.CustomerRegionName,
                    c.IdCustomerCountry,
                    c.CustomerCountryName
                }).OrderBy(m => m.CustomerCountryName);

                foreach (var item in regionsWithTheirCountry)
                {
                    CustomerCountryRegionVM countryRegionVM = new CustomerCountryRegionVM();
                    countryRegionVM.IdCustomerRegion    = item.IdCustomerRegion;
                    countryRegionVM.CustomerRegionName  = item.CustomerRegionName;
                    countryRegionVM.IdCustomerCountry   = item.IdCustomerCountry;
                    countryRegionVM.CustomerCountryName = item.CustomerCountryName;
                    countryRegionList.Add(countryRegionVM);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            return(View(countryRegionList));
        }
        public async Task <IActionResult> Create([Bind("IdCustomerRegion,CustomerRegionName,IdCustomerCountry")] CustomerCountryRegionVM customerCountryRegionVM)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    CustomerRegions region = new CustomerRegions();
                    region.CustomerRegionName = customerCountryRegionVM.CustomerRegionName;
                    region.IdCustomerCountry  = customerCountryRegionVM.IdCustomerCountry;

                    _context.Add(region);
                    await _context.SaveChangesAsync();

                    return(RedirectToAction(nameof(Index)));
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }

            ViewData["IdCustomerCountry"] = new SelectList(_context.CustomerCountries, "IdCustomerCountry", "IdCustomerCountry");

            return(View(customerCountryRegionVM));
        }
        // GET: CustomerRegions/Details/5
        public IActionResult Details(int?id)
        {
            CustomerCountryRegionVM countryRegionVM = new CustomerCountryRegionVM();

            if (id == null)
            {
                return(NotFound());
            }
            else
            {
                try
                {
                    var regionsWithTheirCountry = (from r in _context.CustomerRegions
                                                   join c in _context.CustomerCountries
                                                   on r.IdCustomerCountry equals c.IdCustomerCountry
                                                   where r.IdCustomerRegion == id
                                                   select new
                    {
                        r.IdCustomerRegion,
                        r.CustomerRegionName,
                        c.IdCustomerCountry,
                        c.CustomerCountryName
                    }).FirstOrDefault();

                    if (regionsWithTheirCountry == null)
                    {
                        return(NotFound());
                    }
                    else
                    {
                        countryRegionVM.IdCustomerRegion    = regionsWithTheirCountry.IdCustomerRegion;
                        countryRegionVM.CustomerRegionName  = regionsWithTheirCountry.CustomerRegionName;
                        countryRegionVM.IdCustomerCountry   = regionsWithTheirCountry.IdCustomerCountry;
                        countryRegionVM.CustomerCountryName = regionsWithTheirCountry.CustomerCountryName;
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }

                return(View(countryRegionVM));
            }
        }