public ActionResult Create(int FazioneId, AutoreViewModel autoremodel) { using (var client = new HttpClient()) { var fazioneDto = _fazioneFeRepository.GetFazione(FazioneId); if (fazioneDto == null || autoremodel == null) { ModelState.AddModelError("", "Fazione o Autore sono invalidi. Autore non è stato creato!"); return(View(autoremodel)); } var autore = new Autore { NomeAutore = autoremodel.NomeAutore, NoteVarie = autoremodel.NoteVarie, Pericolosita = autoremodel.Pericolosita }; autore.Fazione = new Fazione { FazioneId = fazioneDto.FazioneId, NomeFazione = fazioneDto.NomeFazione }; client.BaseAddress = new Uri("https://localhost:44357/api/"); var responseTask = client.PostAsJsonAsync("autori", autore); responseTask.Wait(); var result = responseTask.Result; if (result.IsSuccessStatusCode) { var newAutoreTask = result.Content.ReadAsAsync <Autore>(); newAutoreTask.Wait(); var newAutore = newAutoreTask.Result; TempData["SuccessMessage"] = $"Autore {newAutore.NomeAutore}" + $"è stato creato con successo."; return(RedirectToAction("Index", "Autore")); } ModelState.AddModelError("", "Autore non è stato creato"); } return(View(autoremodel)); }
public ActionResult Create(int FazioneId, MandanteViewModel mandanteModel) { using (var client = new HttpClient()) { var fazioneDto = _fazioneFeRepository.GetFazione(FazioneId); if (fazioneDto == null || mandanteModel == null) { ModelState.AddModelError("", "Fazione o Mandante non validi. Impossibile creare Mandante!"); return(View(mandanteModel)); } var mandante = new Mandante { NomeMandante = mandanteModel.NomeMandante }; mandante.Fazione = new Fazione { FazioneId = fazioneDto.FazioneId, NomeFazione = fazioneDto.NomeFazione }; client.BaseAddress = new Uri("https://localhost:44357/api/"); var responseTask = client.PostAsJsonAsync("mandanti", mandante); responseTask.Wait(); var result = responseTask.Result; if (result.IsSuccessStatusCode) { var newMandanteTask = result.Content.ReadAsAsync <Mandante>(); newMandanteTask.Wait(); var newMandante = newMandanteTask.Result; TempData["SuccessMessage"] = $"Mandante {newMandante.NomeMandante}" + $"è stato creato con successo."; return(RedirectToAction("Index", "Mandante")); } ModelState.AddModelError("", "Mandante non creato"); } return(View(mandanteModel)); }
// GET: Fazione/Edit/5 public ActionResult Edit(int fazioneId) { var fazioneToUpdate = _fazioneFeRepository.GetFazione(fazioneId); if (fazioneToUpdate == null) { ModelState.AddModelError("", "Errore durante l'ottenimento della Fazione"); fazioneToUpdate = new FazioneDto(); } return(View(fazioneToUpdate)); }