Asked by:
Save Multiple Data into table

Question
-
User2041008840 posted
Hello
I want to save checked data into table, This is my code.
How Do i solve this.View : <form > <table id="table" class="table" data-toggle="table" data-pagination="true" data-search="true" data-show-columns="true" data-show-export="true" data-page-list="[10, 25, 50, 100, all]" data-sort-class="table-active" data-trim-on-search="false" data-show-columns-toggle-all="true" data-search-align="left" data-responsive="true" data-toolbar="#toolbar" data-show-print="true"> <thead> <tr> <th>Lot No.</th> <th>Part No.</th> <th>Item</th> <th>Job Work Part No.</th> <th>Length</th> <th>Quantity</th> <th>Total</th> <th> <div class="custom-control custom-checkbox"> <input type="checkbox" class="custom-control-input" id="customCheck1" /> <label class="custom-control-label" for="customCheck1">All</label> </div> </th> </tr> </thead> <tbody> @foreach (var item in ViewBag.JobPlan) { <tr> <td>@item.JobRequirement.LotNo</td> <td>@item.JobRequirement.PartNo</td> <td>@item.JobRequirement.Item</td> <td>@item.JobRequirement.JobWorkPartNo</td> <td>@item.JobRequirement.Length</td> <td>@item.JobRequirement.Quantity</td> <td>@(item.JobRequirement.Length * item.JobRequirement.Quantity)</td> <td> <input type="text" value="@item.ID" asp-for="JobPlanID" required /> <div class="custom-control custom-checkbox"> <input type="checkbox" class="custom-control-input" id="customCheck2+@item.ID" /> <label class="custom-control-label" for="customCheck2+@item.ID"></label> </div> </td> </tr> } </tbody> </table> <br /> <input value="Add To DC" type="submit" Class="btn btn-success" /> </form>
public IActionResult Invoice(int? ID, int CustomerID, int FiscalID, int BranchID, DateTime DateFrom, DateTime DateTo) { var exceptionList = from d in _context.DeliveryChallanItem where d.JobPlan.JobRequirement.FiscalID == FiscalID && d.JobPlan.JobRequirement.BranchID == BranchID && d.JobPlan.JobRequirement.CustomerID == CustomerID select d.JobPlanID; ViewBag.JobPlan = from j in _context.JobPlan where !exceptionList.Contains(j.ID) && j.JobRequirement.FiscalID == FiscalID && j.JobRequirement.BranchID == BranchID && j.JobRequirement.CustomerID == CustomerID && (j.CreatedOn >= DateFrom && j.CreatedOn <= DateTo) select j; ViewData["CustomerID"] = new SelectList(_context.Customer, "ID", "Name", CustomerID); ViewData["FiscalID"] = new SelectList(_context.Fiscal.OrderByDescending(a => a.ID), "ID", "FiscalYear", FiscalID); ViewData["DCItems"] = _context.DeliveryChallanItem.Include(a => a.JobPlan).Include(a => a.JobPlan.JobRequirement).Where(a => a.DeliveryChallanID == ID); return View(); } [HttpPost] public async Task<IActionResult> Invoice(int? ID, int CustomerID, int FiscalID, int BranchID, DateTime DateFrom, DateTime DateTo, DeliveryChallanItem deliveryChallanitem) { var exceptionList = from d in _context.DeliveryChallanItem where d.JobPlan.JobRequirement.FiscalID == FiscalID && d.JobPlan.JobRequirement.BranchID == BranchID && d.JobPlan.JobRequirement.CustomerID == CustomerID select d.JobPlanID; var jp = from j in _context.JobPlan where !exceptionList.Contains(j.ID) && j.JobRequirement.FiscalID == FiscalID && j.JobRequirement.BranchID == BranchID && j.JobRequirement.CustomerID == CustomerID && (j.CreatedOn >= DateFrom && j.CreatedOn <= DateTo) select j; ViewBag.JobPlan = jp; _context.DeliveryChallanItem.AddRange(new DeliveryChallanItem { JobPlanID = deliveryChallanitem.JobPlanID, DeliveryChallanID = (int)ID }); await _context.SaveChangesAsync(); ViewData["CustomerID"] = new SelectList(_context.Customer, "ID", "Name", CustomerID); ViewData["FiscalID"] = new SelectList(_context.Fiscal.OrderByDescending(a => a.ID), "ID", "FiscalYear", FiscalID); ViewData["DCItems"] = _context.DeliveryChallanItem.Include(a => a.JobPlan).Include(a => a.JobPlan.JobRequirement).Where(a => a.DeliveryChallanID == ID); return View(deliveryChallanitem); }
Thursday, December 19, 2019 7:55 AM
All replies
-
User475983607 posted
I'm a little confused. The code shown displays records. The only checkbox I see does not have a name attribute which is required when submitting a form.
I recommend going through the following tutorial which illustrates how to insert records in ASP.NET Core\EF Core if you are not sure how to do so. Also, you'll need to adjust your approach as it does not follow standard programming patterns.
https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/intro?view=aspnetcore-3.1
Next, see the following links to learn how to model bind a collection of types which is required to save multiple records.
https://www.learnrazorpages.com/razor-pages/model-binding#binding-simple-collections
https://docs.microsoft.com/en-us/aspnet/core/mvc/models/model-binding?view=aspnetcore-3.1
Thursday, December 19, 2019 11:57 AM -
User2041008840 posted
I think I add wrong version code - please see this, i tried but its not inserting I dont know why,
public IActionResult Invoice(int? ID, int CustomerID, int FiscalID, int BranchID, DateTime DateFrom, DateTime DateTo) { var exceptionList = from d in _context.DeliveryChallanItem where d.JobPlan.JobRequirement.FiscalID == FiscalID && d.JobPlan.JobRequirement.BranchID == BranchID && d.JobPlan.JobRequirement.CustomerID == CustomerID select d.JobPlanID; var jb = (from j in _context.JobPlan where !exceptionList.Contains(j.ID) && j.JobRequirement.FiscalID == FiscalID && j.JobRequirement.BranchID == BranchID && j.JobRequirement.CustomerID == CustomerID && (j.CreatedOn >= DateFrom && j.CreatedOn <= DateTo) select j).ToList(); ViewBag.JobPlan = jb; DeliveryChallanViewModel dc = new DeliveryChallanViewModel(); dc.jobs = jb.Select(vc => new CheckBoxItem() { LotNo = vc.ID, IsChecked = false }).ToList(); ViewData["CustomerID"] = new SelectList(_context.Customer, "ID", "Name", CustomerID); ViewData["FiscalID"] = new SelectList(_context.Fiscal.OrderByDescending(a => a.ID), "ID", "FiscalYear", FiscalID); ViewData["DCItems"] = _context.DeliveryChallanItem.Include(a => a.JobPlan).Include(a => a.JobPlan.JobRequirement).Where(a => a.DeliveryChallanID == ID); return View(dc); } [HttpPost] public async Task<IActionResult> Invoice(int? ID, int CustomerID, int FiscalID, int BranchID, DateTime DateFrom, DateTime DateTo, DeliveryChallanViewModel deliveryChallanViewModel) { var exceptionList = from d in _context.DeliveryChallanItem where d.JobPlan.JobRequirement.FiscalID == FiscalID && d.JobPlan.JobRequirement.BranchID == BranchID && d.JobPlan.JobRequirement.CustomerID == CustomerID select d.JobPlanID; var jp = from j in _context.JobPlan where !exceptionList.Contains(j.ID) && j.JobRequirement.FiscalID == FiscalID && j.JobRequirement.BranchID == BranchID && j.JobRequirement.CustomerID == CustomerID && (j.CreatedOn >= DateFrom && j.CreatedOn <= DateTo) select j; ViewBag.JobPlan = jp; //inserting only checked items into database List<DeliveryChallanItem> dc = new List<DeliveryChallanItem>(); foreach (var item in deliveryChallanViewModel.jobs) { if (item.IsChecked == true) { dc.Add(new DeliveryChallanItem() { JobPlanID = item.ID, DeliveryChallanID = (int)ID }); } } foreach (var item in dc) { _context.DeliveryChallanItem.Add(item); } await _context.SaveChangesAsync(); ViewData["CustomerID"] = new SelectList(_context.Customer, "ID", "Name", CustomerID); ViewData["FiscalID"] = new SelectList(_context.Fiscal.OrderByDescending(a => a.ID), "ID", "FiscalYear", FiscalID); ViewData["DCItems"] = _context.DeliveryChallanItem.Include(a => a.JobPlan).Include(a => a.JobPlan.JobRequirement).Where(a => a.DeliveryChallanID == ID); return View("Index"); }
@model DeliveryChallanViewModel <h1>DC Invoice</h1> <div class="alert alert-dismissible alert-danger" id="pnlAlert" hidden> <button type="button" class="close" data-dismiss="alert">×</button> <strong>Opps !</strong> <a href="#" class="alert-link">Delivery Challan does not contains any lot No.</a> </div> <form> <div class="row"> <div class="col-xl-2 col-lg-2 col-md-2 col-sm-2 col-xs-12"> <label>Customer</label> <select class="selectpicker form-control" data-style="btn-primary" data-live-search="true" id="CustomerID" asp-items="ViewBag.CustomerID" name="CustomerID"> </select> <br /> <br /> <div><input type="submit" class="btn btn-primary" /></div> </div> <div class="col-xl-2 col-lg-2 col-md-2 col-sm-2 col-xs-12"> <label>Branch</label> <select class="form-control" id="BranchID" name="BranchID"> <option value="1" selected>Inner/Outer</option> <option value="2">Cable</option> </select> <br /> <br /> </div> <div class="col-xl-2 col-lg-2 col-md-2 col-sm-2 col-xs-12"> <label>Fiscal Year</label> <select class="form-control" id="FiscalID" name="FiscalID" asp-items="ViewBag.FiscalID"> </select> <br /> <br /> </div> <div class="col-xl-2 col-lg-2 col-md-2 col-sm-2 col-xs-12"> <label>Date From</label> <input class="form-control" value="@DateTime.Now.AddDays(-30).ToString("yyyy-MM-dd")" type="date" id="DateFrom" name="DateFrom" /> </div> <div class="col-xl-2 col-lg-2 col-md-2 col-sm-2 col-xs-12"> <label>Date To</label> <input class="form-control" value="@DateTime.Now.AddDays(1).ToString("yyyy-MM-dd")" type="date" id="DateTo" name="DateTo" /> </div> </div> </form> <form asp-action="Invoice"> <br /> <table id="table" class="table" data-toggle="table" data-pagination="true" data-search="true" data-show-columns="true" data-show-export="true" data-page-list="[10, 25, 50, 100, all]" data-sort-class="table-active" data-trim-on-search="false" data-show-columns-toggle-all="true" data-search-align="left" data-responsive="true" data-toolbar="#toolbar" data-show-print="true"> <thead> <tr> <th>Lot No.</th> <th>Part No.</th> <th>Item</th> <th>Job Work Part No.</th> <th>Length</th> <th>Quantity</th> <th>Total</th> <th> </th> </tr> </thead> <tbody> @foreach (var item in ViewBag.JobPlan) { <tr> <td class="text-right">@item.JobRequirement.LotNo</td> <td>@item.JobRequirement.PartNo</td> <td>@item.JobRequirement.Item</td> <td>@item.JobRequirement.JobWorkPartNo</td> <td class="text-right">@item.JobRequirement.Length</td> <td class="text-right">@item.JobRequirement.Quantity</td> <td class="text-right">@(Convert.ToDouble(item.JobRequirement.Length / 1000 * item.JobRequirement.Quantity).ToString("f3"))</td> <td> @for (int i = 0; i < 1; i++) { <input type="text" value="@item.ID" asp-for="ID" required hidden /> <div class="custom-control custom-checkbox"> <input type="checkbox" asp-for="jobs[i].IsChecked" value="@item.ID" class="custom-control-input case" name="@item.ID" id="@item.ID" /> <label class="custom-control-label" for="@item.ID"></label> </div> } </td> @*<div class="custom-control custom-checkbox"> <input type="checkbox" class="custom-control-input case" name="@item.ID" id="@item.ID" /> <label class="custom-control-label" for="@item.ID"></label> </div>*@ </tr> } </tbody> </table> <br /> <input value="Add To DC" type="submit" Class="btn btn-success" /> </forms>
Thursday, December 19, 2019 1:50 PM -
User475983607 posted
Prathamesh Shende
I think I add wrong version code - please see this, i tried but its not inserting I dont know why,The checkbox has an indexed name but there is no matching action input. There's also a hidden field that submits every ID back to the action and, like the checkbox, there is no input parameter for the ID array. It seems like most of the POST action's input parameters are not used. Why not remove the unneeded inputs? The actual logic is a little confusing without an explanation of how it is supposed to work. Also, it seems like you are posting code without debugging first.
Please, set a side the time to learn the basics rather than posted untested code and forcing the community to go through every line. Please post the actual code under test. I recommend going through the Getting Started tutorial(s) which illustrates how to submit a model. Next, read the other links to learn how to submit/model bind a collection of types. That should give you enough information to fix the code.
Thursday, December 19, 2019 2:07 PM