/// <summary> /// Partial view with section's editing form with corresponding data /// </summary> /// <param name="id">Section's identifier</param> /// <returns>Corresponding view</returns> public async Task <IActionResult> Edit(int?id) { if (id == null) { return(NotFound()); } var section = await _context.Sections.Include(s => s.Location.City).FirstOrDefaultAsync(s => s.Id == id); if (section == null) { return(NotFound()); } var model = new AddEditSectionViewModel { City = section.Location.City.Name, Location = section.Location.Name, Name = section.Name }; ViewData["City"] = new SelectList(_context.Cities, "Name", "Name", model.City); ViewData["Location"] = new SelectList(_context.Locations.Include(l => l.City).Where(l => l.City.Name == model.City), "Name", "Name", model.Location); return(PartialView(model)); }
public IActionResult Post(string section, [FromBody] AddEditSectionViewModel obj) { var city = _context.Cities.FirstOrDefault(c => c.Name == obj.City); var location = _context.Locations.FirstOrDefault(l => l.Name == obj.Location); // return 400, if data is invalid if (city == null || location == null || location.CityId != city.Id) { return(BadRequest()); } var model = _context.Sections.FirstOrDefault(s => s.ShortName == section); // create new section, if such section is not exist if (model == null) { var newSection = new Section { Name = obj.Name, ShortName = section, LocationId = location.Id }; _context.Sections.Add(newSection); } // return 400 in other way else { return(BadRequest()); } _context.SaveChanges(); return(Ok()); }
/// <summary> /// Partial view with information about chosen section /// </summary> /// <param name="id">Section's identifier</param> /// <returns>Corresponding view</returns> public async Task <IActionResult> Details(int?id) { if (id == null) { return(NotFound()); } var section = await _context.Sections .Include(s => s.Location.City) .FirstOrDefaultAsync(m => m.Id == id); if (section == null) { return(NotFound()); } var model = new AddEditSectionViewModel { City = section.Location.City.Name, Location = section.Location.Name, Name = section.Name }; return(PartialView(model)); }
public SectionViewModel(Core.Models.Section section) { Section = section.ShortName; Info = new AddEditSectionViewModel { City = section.Location.City.Name, Location = section.Location.Name, Name = section.Name }; }
public ActionResult <AddEditSectionViewModel> Get(string section) { var model = _context.Sections.Include(s => s.Location.City).FirstOrDefault(s => s.ShortName == section); // return 404, if sections with such abbreviation is not exist if (model == null) { return(NotFound()); } var viewModel = new AddEditSectionViewModel { City = model.Location.City.Name, Location = model.Location.Name, Name = model.Name }; return(viewModel); }