Hello,
I wonder if the following is possible. I have a view which has multiple tab-panes. Each tab-pane correspond to aStore.
Inside each Store there is a table which contains information of all the Machines assigned to that Store . The information of each Machine (each Machine has a selling Device) is presented in a row. Each row is wrapped as a individual form.
This is basically a sales report, so in each row the user can introduce how many sells were made for each Device.
The objetive:
I'm trying to submit all the records at a
Storelevel. I mean, for each Store submit all the results introduced for each one of the Machine in that store.
Here is the view:
This is the code, simplified:
Models:
Sales:
public class Sales { [Key] public int SaleID { get; set; } public string DeviceName { get; set; } public int SalesAmount { get; set; } //foreign keys public int? DeviceID { get; set; } public int? MachineId { get; set; } public int StoreID { get; set; } [ForeignKey("DeviceID")] public virtual Device Devices{ get; set; } [ForeignKey("StoreID")] public virtual Store Stores { get; set; } }
SalesIndexData (ViewModel)
public class SalesIndexData { [Key] public int SaleID { get; set; } public string DeviceName { get; set; } public int SalesAmount { get; set; } public int DeviceID { get; set; } public int MachineId { get; set; } public int StoreID { get; set; } //Lists public List<Store> StoreL { get; set; } public List<Sales> SalesL { get; set; } }
Get Action:
public async Task<IActionResult> Index() { var Tiendas = await _context.Stores.Where(t => t.Usuario == User.Identity.Name) .Include(t => t.Machines).ThenInclude(t=>t.MachineTypes).ThenInclude(t=>t.MachineFamilies) .Include(t => t.Machines).ThenInclude(t => t.Devices) .Include(t => t.Machines).ThenInclude(t => t.MachineTypeIncomes) .Include(t => t.HechosLiquidadors).ToListAsync(); SalesIndexData Liquid = new SalesIndexData() { StoreL = Tiendas, }; return View(Liquid); }
View:
@model Application.Models.ApplicationviewModels.LiqIndexData<div class="container"><ul class="nav nav-pills"> @foreach (var item in Model.StoreL) { @if (item.StoreLastLiq != Model.CurrentDate) {<li><a href="#@item.StoreID" data-toggle="tab">@item.StoreName</a></li> } }</ul><div class="tab-content"> @foreach (var item in Model.StoreL) {<div class="tab-pane fade" id="@item.StoreID"><table style="width:100%;" class="table table-striped table-bordered table-hover"><thead><tr><td hidden></td><td><div>Machine Model</div></td><td><div>Sales</div></td></tr></thead><tbody> @foreach (var store in Model.StoreL.Where(s => s.StoreID == item.StoreID)) { @foreach (var machine in store.Machines) { @foreach (var device in machine.Devices) {<tr><td hidden><form id="@(String.Format("{0}{1}","form",device.DeviceID))" asp-action="AddLiquidacion" asp-route-id="@device.DeviceID" method="post"></form></td><td form="@(String.Format("{0}{1}","form",device.DeviceID))"> @Html.DisplayFor(modelItem => machine.CoinValue)</td><td><div class="form-group"><div><input id="@device.DeviceName" form="@(String.Format("{0}{1}","form",device.DeviceID))" type="number" min="0" step="1" asp-for="@Model.SalesAmount" name="SalesAmount" /><span asp-validation-for="@Model.SalesAmount" class="text-danger field-validation-valid" data-valmsg-for="Cortesia" data-valmsg-replace="true"></span></div></div></td></tr> } } }</tbody></table><div class="btn-group"><a id="AddLiquidacion" asp-action="Index" method="post" class="btn btn-default"> Grabar Liquidación</a></div></div> } }
Question:
I couldn't test yet if having a submit button for each tab-pane is enough. I have to make sure that the information from the table is going to the Controller.
Since there are many forms, and each forms constitutes or takes the form of the SalesIndexData, I thought about sending a List to the Controller:
[HttpPost, ActionName("Index")] [ValidateAntiForgeryToken] public async Task<IActionResult> IndexPost(List<LiqIndexData> LiquidacionList)
But how can I define that each item on that List is made of each row from the table? Each row is wrapped in a form, and all the fields in each row share the same id, if this helps.
If I can populate the List I believe is possible to then iterate thru the list, assign each property to the corresponding property in the Sales class and add this to the context.
Thanks in advance for any help / comment.