Exemple #1
0
        public async Task <ActionResult> AddorEdit([Bind("Name, CountryCode, District, Population")] City city)
        {
            var context     = new worldContext();
            var currentCity = context.City.Where(c => c.Name.Equals(city.Name)).FirstOrDefault();

            if (ModelState.IsValid)
            {
                if (currentCity == null)
                {
                    context.Add(city);
                }
                else
                {
                    currentCity.Name        = city.Name;
                    currentCity.CountryCode = city.CountryCode;
                    currentCity.District    = city.District;
                    currentCity.Population  = city.Population;
                    context.Update(currentCity);
                }
                await context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(city));
        }
 public async Task <IActionResult> Create(City city)
 {
     if (ModelState.IsValid)
     {
         _context.Add(city);
         await _context.SaveChangesAsync();
     }
     return(RedirectToAction(nameof(Index)));
 }
        public async Task <IActionResult> Create([Bind("Code,Name,Region,NationalFlag")] Country country)
        {
            if (ModelState.IsValid)
            {
                _context.Add(country);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(country));
        }
Exemple #4
0
        public async Task <IActionResult> Create([Bind("Id,Name,CountryCode,District,Population")] City city)
        {
            if (ModelState.IsValid)
            {
                _context.Add(city);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["CountryCode"] = new SelectList(_context.Country, "Code", "Code", city.CountryCode);
            return(View(city));
        }
 public async Task <IActionResult> Create(Country country, IFormFile nationalFlagFile)
 {
     //Divide this code, it violates SRP and DRY
     if (ModelState.IsValid)
     {
         if (nationalFlagFile != null && nationalFlagFile.Length != 0)
         {
             var targetFileName = $"{country.Code}{Path.GetExtension(nationalFlagFile.FileName)}";
             var relativePath   = Path.Combine("images", targetFileName);
             var absolutePath   = Path.Combine(_environment.WebRootPath, relativePath);
             country.NationalFlag = relativePath;
             using (var stream = new FileStream(absolutePath, FileMode.Create))
             {
                 await nationalFlagFile.CopyToAsync(stream);
             }
             _context.Add(country);
             await _context.SaveChangesAsync();
         }
     }
     return(RedirectToAction(nameof(Index)));
 }
Exemple #6
0
        public async Task <ActionResult> AddorEdit([Bind("Code, Name, Region, NationalFlag")] Country country)
        {
            var context        = new worldContext();
            var currentCountry = context.Country.Find(country.Code);

            if (ModelState.IsValid)
            {
                if (currentCountry == null)
                {
                    context.Add(country);
                }
                else
                {
                    currentCountry.Name         = country.Name;
                    currentCountry.Region       = country.Region;
                    currentCountry.NationalFlag = country.NationalFlag;
                    context.Update(currentCountry);
                }
                await context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(country));
        }