Answered by:
Value of Foreach Loop does not pass to the HttpPost controller action model

Question
-
User210895818 posted
Hi I have separated the list of students based on their department then try to edit their all students of the same DEPARTMENT on a one-click button but when i tried to pass data from foreach loop to the controller data does not pass to the model.
Here is my controller:
public ActionResult AllotRegistration() { StudentSearchModel depart = new StudentSearchModel(); ViewBag.DepartmentList = Context.Departments.Where(a=>a.Description == "Academic").ToList(); List<StdListModel> model = new List<StdListModel>(); var query = (from e in Context.StdEnrollments join d in Context.StdDetails on e.StdReg_ref_id equals d.ID join s in Context.DegreePrograms on e.DegreeProgram_ref_id equals s.ID // where e.Depart_ref_id == value.Depart_ref_id where s.StartYear == "2019" && e.Program_ref_id == 1 && e.Status == "Active" select new StdListModel { ID = e.ID, Registration =e.Resgistration }).ToList(); foreach (var item in query) { model.Add(new StdListModel() { ID = item.ID, Registration = item.Registration }); } return View(model); } [HttpPost] public ActionResult AllotRegistration(List<StdListModel> model) { ViewBag.DepartmentList = Context.Departments.Where(a => a.Description == "Academic").ToList(); foreach (var i in model) { var c = Context.StdEnrollments.Where(a => a.ID.Equals(i.ID)).FirstOrDefault(); c.Resgistration = i.Registration; } Context.SaveChanges(); return View(model); }
and my View is as:
@model List<project.Models.StdListModel> @{ ViewBag.Title = "AppliedStdDetail"; ViewBag.Module = "Department"; ViewBag.Screen = "AppliedStudents"; ViewBag.MenuID = "submun_studentDetails"; Layout = "~/Views/Shared/_SRSLayout.cshtml"; } <div class="page-content"> <div class="page-header"> <h1> Student Registration Panel </h1> </div> <div class="col-xs-10"> <div class="panel-group" id="accordion"> @{ int j = 0; } @foreach (var item in @ViewBag.DepartmentList) { <div class="panel panel-default" style="margin-top:5px;"> <div class="panel-heading" style="background-color:cornflowerblue;"> <h4 class="panel-title"> <a data-toggle="collapse" data-parent="#accordion" href="#collapse-@j" aria-expanded="false" style="color:white"> @item.DepartName </a> </h4> </div> <div id="collapse-@j" class="panel-collapse collapse"> <div class="panel-body"> @using (Html.BeginForm("AllotRegistration", "Student", FormMethod.Post)) { <table id="dynamic-table" class="table table-striped table-bordered table-hover" name="StudentDetail"> <thead> <tr> @*<th>Actions</th>*@ <th>Registration</th> </tr> </thead> <tbody> @foreach (var i in Model.Where(s => s.Depart_ref_id == item.ID).ToList()) { <tr> <input type="hidden" name="ID" value="@i.ID" /> <td> @if (i.Registration != "0") { @Html.DisplayFor(model => i.Registration) } else { <input name="Registration" value="@i.Registration" /> @*@Html.EditorFor(model => i.Registration)*@ } </td> </tr> } </tbody> </table> <p> <input type="submit" value="Submit" /> </p> } </div> </div> </div> j++; } </div> </div> </div>
Thursday, November 7, 2019 11:09 AM
Answers
-
User665608656 posted
Hi Ridzi,
Thanks, Yongqing for your reply, but I can't use for loop because on a condition "where" clause does not work. Can you please guide me with foreach loop or why where clause is not working in for loop?I'm very sorry that I didn't notice this issue. After some tests, I suggest that you can prevent submit form events, and then complete these operations in jquery's ajax.
<script src="~/Scripts/jquery-3.3.1.js"></script> <script type="text/javascript"> $(function () { $("input[type='submit']").click(function () { event.preventDefault(); var table = $(this).parent("p").prev("table"); var idlist = []; var namelist = [];
// use loop to find fields you need to pass for (var i = 0; i < table.find("input").length; i++) { if (table.find("input")[i].name.includes("ID")) { idlist.push(table.find("input")[i].value); } if (table.find("input")[i].name.includes("Registration")) { namelist.push(table.find("input")[i].value); } } var model = []; for (var i = 0; i < idlist.length; i++) { model.push({ ID: idlist[i], Registration: namelist[i] }); } $.ajax({ contentType: 'application/json; charset=utf-8', dataType: 'json', type: 'POST', url: '/Student/AllotRegistration', data: JSON.stringify({ 'model': model }) }); }) }) </script>And here is the code of bind data:
@for (int i = 0; i < Model.Count; i++) { if (Model[i].Depart_ref_id == item.ID) { <tr> @Html.HiddenFor(model => Model[i].ID ) <td> @if (Model[i].Registration != "0") { @Html.DisplayFor(model => Model[i].Registration) @Html.HiddenFor(model => Model[i].Registration) } else { @Html.EditorFor(model => Model[i].Registration) } </td> </tr> } }
Best Regards,
YongQing.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, November 11, 2019 2:13 AM
All replies
-
User475983607 posted
The input names must be indexed for the model binder to populate the collection.
https://haacked.com/archive/2010/07/16/uploading-files-with-aspnetmvc.aspx/
Thursday, November 7, 2019 11:20 AM -
User210895818 posted
Hi Mgebhard thank for your reply but I need to do this with foreach loop because with for loop condition where clause does not work
@for (int i = 0; i < Model.Where(a=>a.Depart_ref_id == item.ID)).Count(); i++)
Friday, November 8, 2019 4:08 AM -
User665608656 posted
Hi Ridzi,
According to your description, after you use for loop to fill table data, you also need to add a hidden control to store value which displays as label not textbox.
Text tags don't pass values when you submit a form, but you can pass them by adding hidden controls, you can refer to this link : https://stackoverflow.com/a/44628875
Here is the code:
@for (int i = 0; i < Model.Where(s => s.Depart_ref_id == item.ID).ToList().Count; i++) { <tr> @Html.HiddenFor(model => Model[i].ID) <td> @if (Model[i].Registration != "0") { @Html.DisplayFor(model => Model[i].Registration) @Html.HiddenFor(model => Model[i].Registration) } else { @Html.EditorFor(model => Model[i].Registration) } </td> </tr> }
Best Regards,
YongQing.
Friday, November 8, 2019 5:39 AM -
User210895818 posted
Yongqing Yu
after you use for loop to fill table data,Thanks, Yongqing for your reply, but I can't use for loop because on a condition "where" clause does not work. Can you please guide me with foreach loop or why where clause is not working in for loop?
Friday, November 8, 2019 7:05 AM -
User665608656 posted
Hi Ridzi,
Thanks, Yongqing for your reply, but I can't use for loop because on a condition "where" clause does not work. Can you please guide me with foreach loop or why where clause is not working in for loop?I'm very sorry that I didn't notice this issue. After some tests, I suggest that you can prevent submit form events, and then complete these operations in jquery's ajax.
<script src="~/Scripts/jquery-3.3.1.js"></script> <script type="text/javascript"> $(function () { $("input[type='submit']").click(function () { event.preventDefault(); var table = $(this).parent("p").prev("table"); var idlist = []; var namelist = [];
// use loop to find fields you need to pass for (var i = 0; i < table.find("input").length; i++) { if (table.find("input")[i].name.includes("ID")) { idlist.push(table.find("input")[i].value); } if (table.find("input")[i].name.includes("Registration")) { namelist.push(table.find("input")[i].value); } } var model = []; for (var i = 0; i < idlist.length; i++) { model.push({ ID: idlist[i], Registration: namelist[i] }); } $.ajax({ contentType: 'application/json; charset=utf-8', dataType: 'json', type: 'POST', url: '/Student/AllotRegistration', data: JSON.stringify({ 'model': model }) }); }) }) </script>And here is the code of bind data:
@for (int i = 0; i < Model.Count; i++) { if (Model[i].Depart_ref_id == item.ID) { <tr> @Html.HiddenFor(model => Model[i].ID ) <td> @if (Model[i].Registration != "0") { @Html.DisplayFor(model => Model[i].Registration) @Html.HiddenFor(model => Model[i].Registration) } else { @Html.EditorFor(model => Model[i].Registration) } </td> </tr> } }
Best Regards,
YongQing.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, November 11, 2019 2:13 AM -
User210895818 posted
Hi Yongqing, thanks for your help
Monday, November 11, 2019 6:26 AM