From 2b536cc7dd812fd6c1ea50ddcd314385eab7fd0d Mon Sep 17 00:00:00 2001 From: Marco Audiffredi Date: Mon, 8 Jul 2024 18:07:41 +0200 Subject: [PATCH 1/2] lista con checkbox --- Controllers/GiriController.cs | 52 +++++++++++++++++++-- Models/GiriConsegnaDaCreare.cs | 2 + Views/Giri/Create.cshtml | 82 ++++++++++++++++++++++++++++++++-- 3 files changed, 130 insertions(+), 6 deletions(-) diff --git a/Controllers/GiriController.cs b/Controllers/GiriController.cs index 1a065bd..cca0f32 100644 --- a/Controllers/GiriController.cs +++ b/Controllers/GiriController.cs @@ -80,11 +80,57 @@ namespace SoftwayWeb.Controllers public IActionResult Create() { SessionHelper helper = new SessionHelper(this); - ViewBag.Autisti = getAutisti(); - ViewBag.NuoviGiri = getNuoviGiri(); - return View(); + List modelList = new List(); + apiUrl = helper.GetStringValue("apiUrl"); + urlBase = apiUrl + "Giri/listaGiriDaCreare"; + + Uri baseAddress = new Uri(urlBase); + client = new HttpClient(); + client.BaseAddress = baseAddress; + + HttpResponseMessage response = client.GetAsync(baseAddress).Result; + if (response.IsSuccessStatusCode) + { + string data = response.Content.ReadAsStringAsync().Result; + modelList = JsonConvert.DeserializeObject>(data); + } + //ViewBag.Autisti = getAutisti(); + //ViewBag.NuoviGiri = getNuoviGiri(); + return View(modelList); } + [HttpPost] + public IActionResult PostIndex(IList lst) + { + SessionHelper helper = new SessionHelper(this); + foreach(GiriConsegnaDaCreare g in lst) + { + if(g.IsSelected==true) + { + //ViewBag.Autisti = getAutisti(); + apiUrl = helper.GetStringValue("apiUrl"); + urlBase = apiUrl + "Giri/addGiro2"; + + Uri baseAddress = new Uri(urlBase); + client = new HttpClient(); + client.BaseAddress = baseAddress; + string data = JsonConvert.SerializeObject(g); + StringContent content = new StringContent(data, Encoding.UTF8, "application/json"); + HttpResponseMessage response = client.PostAsync(baseAddress, content).Result; + if (response.IsSuccessStatusCode) + { + + } + else + { + errMes = response.Content.ReadAsStringAsync().Result; + helper.SetStringValue("errMsg", errMes); + return RedirectToAction("Error"); + } + } + } + return RedirectToAction("Index", "Giri"); + } [HttpPost] public IActionResult Create(GiriConsegnaView model) { diff --git a/Models/GiriConsegnaDaCreare.cs b/Models/GiriConsegnaDaCreare.cs index 2c38477..3fd8544 100644 --- a/Models/GiriConsegnaDaCreare.cs +++ b/Models/GiriConsegnaDaCreare.cs @@ -11,5 +11,7 @@ namespace SoftwayWeb.Models [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")] public DateTime? DataGiro { get; set; } + + public bool IsSelected { get; set; } } } diff --git a/Views/Giri/Create.cshtml b/Views/Giri/Create.cshtml index c154730..8a8e044 100644 --- a/Views/Giri/Create.cshtml +++ b/Views/Giri/Create.cshtml @@ -1,7 +1,9 @@ -@model SoftwayWeb.Models.GiriConsegnaView +@model List @{ ViewData["Title"] = "Create"; Layout = "~/Views/Shared/_LayoutAreaRis.cshtml"; + List lst = new List(); + lst = Model; }
@@ -50,9 +52,11 @@
- +@*
+ + @@ -61,14 +65,86 @@ @foreach (var person in ViewBag.NuoviGiri) { + + } -
   Data Autista Automezzo
  @person.DataGiro.ToString("dd/MM/yyyy") @person.CodAutista - @person.Autista @person.CodAutomezzo - @person.Automezzo
+ *@ + +@* @using (Html.BeginForm("PostIndex", "Giri", FormMethod.Post)) + {*@ +
+ + + + + + + + + + + @for (var i = 0; i < lst.Count(); i++) + { + + + + + + @Html.HiddenFor(a=>lst[i].Automezzo) + @Html.HiddenFor(a=>lst[i].Autista) + + + } + +@* @for (var i = 0; i < lst.Count(); i++) + { + string a = lst[i].CodAutista; + string d = lst[i].DataGiro.Value.ToString("dd/MM/yyyy"); + string m = lst[i].CodAutomezzo; + + + + + + + + }*@ + +
 DataAutistaAutomezzo
+ @Html.CheckBoxFor(a=>lst[i].IsSelected) + + + @Html.DisplayFor(a=>lst[i].DataGiro) + @Html.HiddenFor(a=>lst[i].DataGiro) + + + @Html.DisplayFor(a=>lst[i].CodAutista)-@Html.DisplayFor(a=>lst[i].Autista) + @Html.HiddenFor(a=>lst[i].CodAutista) + + + @Html.DisplayFor(a=>lst[i].CodAutomezzo)-@Html.DisplayFor(a=>lst[i].Automezzo) + @Html.HiddenFor(a=>lst[i].CodAutomezzo) + +
+ @Html.CheckBoxFor(m => [i].Selected); + + @d + + @a + + @m +
+ +
+ + +
@section Scripts { From 700dc5317ef1c7d0a110c4a5fc0f0705a1b9314e Mon Sep 17 00:00:00 2001 From: Marco Audiffredi Date: Tue, 9 Jul 2024 10:28:37 +0200 Subject: [PATCH 2/2] inserimento nuovo giro e selezioa e deseleziona --- Controllers/GiriController.cs | 12 ++++++-- Models/GiriConsegnaDaCreare.cs | 2 +- Views/Giri/Create.cshtml | 53 ++++------------------------------ Views/Giri/Index.cshtml | 2 +- 4 files changed, 16 insertions(+), 53 deletions(-) diff --git a/Controllers/GiriController.cs b/Controllers/GiriController.cs index cecc974..e6f5dd9 100644 --- a/Controllers/GiriController.cs +++ b/Controllers/GiriController.cs @@ -85,7 +85,9 @@ namespace SoftwayWeb.Controllers } } - public IActionResult Create() + + + public IActionResult Create(bool sel) { SessionHelper helper = new SessionHelper(this); List modelList = new List(); @@ -102,8 +104,12 @@ namespace SoftwayWeb.Controllers string data = response.Content.ReadAsStringAsync().Result; modelList = JsonConvert.DeserializeObject>(data); } - //ViewBag.Autisti = getAutisti(); - //ViewBag.NuoviGiri = getNuoviGiri(); + + foreach(GiriConsegnaDaCreare giro in modelList) + { + giro.IsSelected= sel; + } + return View(modelList); } diff --git a/Models/GiriConsegnaDaCreare.cs b/Models/GiriConsegnaDaCreare.cs index 3fd8544..64d4728 100644 --- a/Models/GiriConsegnaDaCreare.cs +++ b/Models/GiriConsegnaDaCreare.cs @@ -4,7 +4,7 @@ namespace SoftwayWeb.Models { public class GiriConsegnaDaCreare { - public string? CodAutomezzo { get; set; } + public string? CodMezzo { get; set; } public string? CodAutista { get; set; } public string? Autista { get; set; } public string? Automezzo { get; set; } diff --git a/Views/Giri/Create.cshtml b/Views/Giri/Create.cshtml index 8a8e044..e38305b 100644 --- a/Views/Giri/Create.cshtml +++ b/Views/Giri/Create.cshtml @@ -52,32 +52,7 @@
-@* - - - - - - - - - - - @foreach (var person in ViewBag.NuoviGiri) - { - - - - - - - } - -
  DataAutistaAutomezzo
 @person.DataGiro.ToString("dd/MM/yyyy")@person.CodAutista - @person.Autista@person.CodAutomezzo - @person.Automezzo
*@ - -@* @using (Html.BeginForm("PostIndex", "Giri", FormMethod.Post)) - {*@
@@ -107,8 +82,8 @@ @Html.HiddenFor(a=>lst[i].Automezzo) @@ -117,32 +92,14 @@ } -@* @for (var i = 0; i < lst.Count(); i++) - { - string a = lst[i].CodAutista; - string d = lst[i].DataGiro.Value.ToString("dd/MM/yyyy"); - string m = lst[i].CodAutomezzo; - - - - - - - }*@
- @Html.DisplayFor(a=>lst[i].CodAutomezzo)-@Html.DisplayFor(a=>lst[i].Automezzo) - @Html.HiddenFor(a=>lst[i].CodAutomezzo) + @Html.DisplayFor(a=>lst[i].CodMezzo)-@Html.DisplayFor(a=>lst[i].Automezzo) + @Html.HiddenFor(a=>lst[i].CodMezzo)
- @Html.CheckBoxFor(m => [i].Selected); - - @d - - @a - - @m -
- + @Html.ActionLink("Seleziona tutti", "Create", "Giri",new { sel = true }, null) +   + @Html.ActionLink("Deleziona tutti", "Create", "Giri",new { sel = false }, null)
diff --git a/Views/Giri/Index.cshtml b/Views/Giri/Index.cshtml index 54eff59..3900836 100644 --- a/Views/Giri/Index.cshtml +++ b/Views/Giri/Index.cshtml @@ -95,7 +95,7 @@ @Html.DisplayFor(modelItem => item.CodAutista) - @Html.DisplayFor(modelItem => item.Autista) - @Html.HiddenFor(modelItem => item.CodMezzo) - @Html.HiddenFor(modelItem => item.Automezzo) + @Html.DisplayFor(modelItem => item.CodMezzo) - @Html.DisplayFor(modelItem => item.Automezzo) @Html.DisplayFor(modelItem => item.BancaliCaricati)