public IActionResult Show(int id) { var ort = context.Ort.FirstOrDefault(i => i.Id == id); if (ort == null) { return(RedirectToAction("Index")); } OrtViewModel model = new OrtViewModel() { Name = ort.Name, PLZ = ort.PLZ, Kanton = ort.Kanton }; return(View(model)); }
public IActionResult Store(OrtViewModel model) { if (!ModelState.IsValid) { return(View("Create", model)); } Ort ort = new Ort() { Name = model.Name, PLZ = model.PLZ, Kanton = model.Kanton }; context.Ort.Add(ort); context.SaveChanges(); return(RedirectToAction("Show", new { ort.Id })); }
// GET: /<controller>/ public IActionResult Index() { var orte = context.Ort.AsEnumerable(); List <OrtViewModel> models = new List <OrtViewModel>(); foreach (var ort in orte) { OrtViewModel model = new OrtViewModel() { Id = ort.Id, Name = ort.Name, PLZ = ort.PLZ, Kanton = ort.Kanton }; models.Add(model); } return(View(models)); }
public IActionResult Update(OrtViewModel model, int id) { if (!ModelState.IsValid) { return(View("Edit", model)); } var ort = context.Ort.FirstOrDefault(i => i.Id == id); if (ort == null) { return(RedirectToAction("Index")); } ort.PLZ = model.PLZ; ort.Name = model.Name; ort.Kanton = model.Kanton; context.Ort.Update(ort); context.SaveChanges(); return(RedirectToAction("Show", new { id })); }