From e84103edbc00e323669013982e2a8074981bc42a Mon Sep 17 00:00:00 2001 From: michele Date: Mon, 6 Oct 2025 16:58:03 +0200 Subject: [PATCH] Aggiunta dialog quando salvo dati in create e edit dati azienda --- Controllers/DatiAziendaController.cs | 40 ++++++++---- Views/DatiAzienda/Create.cshtml | 65 ++++++++++++++++--- Views/DatiAzienda/Dialog.cshtml | 61 ++++++++++++++++++ Views/DatiAzienda/Edit.cshtml | 94 +++++++++++++++++++++------- 4 files changed, 218 insertions(+), 42 deletions(-) create mode 100644 Views/DatiAzienda/Dialog.cshtml diff --git a/Controllers/DatiAziendaController.cs b/Controllers/DatiAziendaController.cs index 0657f95..4429139 100644 --- a/Controllers/DatiAziendaController.cs +++ b/Controllers/DatiAziendaController.cs @@ -40,6 +40,9 @@ namespace VirtualTask.Controllers _urlLoghi = _configuration["ApplicationInsights:rootUrlApi2"]; } + + #region INDEX + public IActionResult Index() { SessionHelper helper = new SessionHelper(this); @@ -78,6 +81,7 @@ namespace VirtualTask.Controllers return RedirectToAction("Error"); } } + #endregion #region CREATE @@ -96,28 +100,29 @@ namespace VirtualTask.Controllers SessionHelper helper = new SessionHelper(this); string token = helper.GetStringValue("tok"); string tenant2 = helper.GetStringValue("tenant2"); // tenant = azienda - string apiUrl = helper.GetStringValue("apiUrl"); string admin = helper.GetStringValue("admin"); + ViewBag.Admin = admin; ViewBag.AllTecnici = getTecnici(); + // ❌ Validazione fallita → restituisco errori in JSON if (!ModelState.IsValid) - return View(model); + { + var errors = ModelState.Values.SelectMany(v => v.Errors).Select(e => e.ErrorMessage).ToList(); + return BadRequest(string.Join("\n", errors)); + } string logoUrl = null; - // 1️⃣ Upload del logo tramite API UploadLogo // 1️⃣ Upload del logo tramite API UploadLogo if (model.logo != null && model.logo.Length > 0) { - // ✅ Token nella query string string uploadUrl = $"{apiUrl}datiazienda/upload_logo?token={token}"; using (var httpClient = new HttpClient()) using (var form = new MultipartFormDataContent()) { - // ⚡ Solo il file nel multipart var fileContent = new StreamContent(model.logo.OpenReadStream()); fileContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue( string.IsNullOrWhiteSpace(model.logo.ContentType) ? "application/octet-stream" : model.logo.ContentType @@ -129,17 +134,15 @@ namespace VirtualTask.Controllers if (!response.IsSuccessStatusCode) { string err = await response.Content.ReadAsStringAsync(); - ModelState.AddModelError("", "Errore upload logo: " + err); - return View(model); + return BadRequest("Errore upload logo: " + err); } dynamic result = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync()); logoUrl = result.url; } - } - // 2️⃣ Creazione oggetto da inviare all'API datiazienda/add + // 2️⃣ Creo l'oggetto da inviare all'API var dat = new DatiAziendaTable { azienda = tenant2, @@ -161,12 +164,20 @@ namespace VirtualTask.Controllers if (!saveResponse.IsSuccessStatusCode) { string err = await saveResponse.Content.ReadAsStringAsync(); - ModelState.AddModelError("", "Errore salvataggio dati: " + err); - return View(model); + return BadRequest("Errore salvataggio dati: " + err); } } - return RedirectToAction("Index"); + // ✅ Risposta JSON usata dalla view per aprire la finestra popup + return Json(new { success = true }); + } + + /// + /// Pagina popup mostrata dopo il salvataggio + /// + public IActionResult Dialog() + { + return View(); } #endregion CREATE @@ -274,7 +285,10 @@ namespace VirtualTask.Controllers } } - return RedirectToAction("Index"); + //return RedirectToAction("Index"); + // ✅ Risposta OK per far apparire la dialog nella view + return Ok(); + } #endregion diff --git a/Views/DatiAzienda/Create.cshtml b/Views/DatiAzienda/Create.cshtml index 5f693e7..cd5d490 100644 --- a/Views/DatiAzienda/Create.cshtml +++ b/Views/DatiAzienda/Create.cshtml @@ -4,61 +4,110 @@ ViewData["Title"] = "Nuova Intestazione Buoni Intervento"; Layout = "~/Views/Shared/_LayoutAreaRiservata.cshtml"; } +
-
+
@Html.HiddenFor(x => x.azienda) @Html.HiddenFor(x => x.url_logo) +
- @Html.DropDownListFor(x =>x.tecnico,(IEnumerable)ViewBag.AllTecnici,new {@class = "form-control"}) + @Html.DropDownListFor(x => x.tecnico, (IEnumerable)ViewBag.AllTecnici, new { @class = "form-control" })
+
 
+
+
 
+
+
 
+
- @**@
+
 
+
-
- @* Torna alla lista *@ -
+ + @section Scripts { - @{await Html.RenderPartialAsync("_ValidationScriptsPartial");} + @{ + await Html.RenderPartialAsync("_ValidationScriptsPartial"); + } } diff --git a/Views/DatiAzienda/Dialog.cshtml b/Views/DatiAzienda/Dialog.cshtml new file mode 100644 index 0000000..af373ca --- /dev/null +++ b/Views/DatiAzienda/Dialog.cshtml @@ -0,0 +1,61 @@ +@{ + Layout = null; +} + + + + + + Dati salvati + + + +

Dati salvati correttamente ✅

+

I dati non saranno visibili nell'app finché non effettuerai il logout e login.

+ + + + + + diff --git a/Views/DatiAzienda/Edit.cshtml b/Views/DatiAzienda/Edit.cshtml index 8ac4639..6621c86 100644 --- a/Views/DatiAzienda/Edit.cshtml +++ b/Views/DatiAzienda/Edit.cshtml @@ -10,61 +10,113 @@
-
+
@Html.HiddenFor(x => x.azienda) @Html.HiddenFor(x => x.url_logo) @Html.HiddenFor(x => x.logo)
-
- @Html.DropDownListFor(x => x.tecnico,(IEnumerable)ViewBag.AllTecnici, new {@class = "form-control"}) +
+ @Html.DropDownListFor(x => x.tecnico, (IEnumerable)ViewBag.AllTecnici, new { @class = "form-control" })
-
 
+
-
+
-
 
+
-
+
@{ - byte[] appo = Model.logo; - var base64 = Convert.ToBase64String(appo); - var imgSrc = String.Format("data:image/gif;base64,{0}", base64); + if (Model.logo != null && Model.logo.Length > 0) + { + var base64 = Convert.ToBase64String(Model.logo); + var imgSrc = $"data:image/png;base64,{base64}"; + + } } -
-
 
+
-
- +
+
-
-
- @* Back to List *@ +
+
+ + + + + + - @section Scripts { - @{await Html.RenderPartialAsync("_ValidationScriptsPartial");} + @{ + await Html.RenderPartialAsync("_ValidationScriptsPartial"); + } + }