Exemple #1
0
        public static StreetItemViewModel Create(Street street)
        {
            var item = new StreetItemViewModel
            {
                Id     = street.Id,
                Name   = street.Name,
                CityId = street.CityId
            };

            return(item);
        }
        public static StreetItemViewModel Create(Street street)
        {
            var item = new StreetItemViewModel
            {
                Id = street.Id,
                Name = street.Name,
                CityId = street.CityId
            };

            return item;
        }
        public IActionResult Create(StreetItemViewModel model)
        {
            if (ModelState.IsValid)
            {
                var existingStreet = _context.Streets.FirstOrDefault(x => x.CityId == model.CityId && x.Name.ToLower() == model.Name.ToLower());
                if (existingStreet != null)
                {
                    ErrorMessage("Улица с таким именем уже существует!");
                    model.CityId = model.CityId;
                    return View("Save", model);
                }

                var street = new Street
                {
                    Name = model.Name,
                    CityId = model.CityId
                };
                _context.Streets.Add(street);
                _context.SaveChanges();

                var editUrl = Url.Action("Edit", new { id = street.Id });
                SuccessMessage($"<a href=\"{editUrl}\">Запись</a> была создана");
                return RedirectToAction("Index");
            }
            return View("Save", model);
        }
        public IActionResult Edit(StreetItemViewModel model)
        {
            if (ModelState.IsValid)
            {
                Street street = _context.Streets.Include(x => x.City).Single(m => m.Id == model.Id);
                street.Name = model.Name;
                street.CityId = model.CityId;
                _context.Update(street);
                _context.SaveChanges();

                var editUrl = Url.Action("Edit", new { id = model.Id });
                SuccessMessage($"<a href=\"{editUrl}\">Запись</a> была успешно изменена");

                return RedirectToAction("Index");
            }
            
            return View(model);
        }
 // GET: Streets/Create
 public IActionResult Create()
 {
     var model = new StreetItemViewModel()
     {
          Name = "",
          CityId = 0
     };
     return View("Save", model);
 }