예제 #1
0
        public async Task <IActionResult> PutCity(int id, City city)
        {
            if (id != city.Id)
            {
                return(BadRequest());
            }

            _context.Entry(city).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CityExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
예제 #2
0
        public async Task <City> Create(City city)
        {
            _context.Cities.Add(city);
            await _context.SaveChangesAsync();

            return(city);
        }
예제 #3
0
 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));
        }
예제 #5
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));
        }
예제 #6
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(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)));
 }
예제 #8
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));
        }