Quick question.
I am trying to add multiple rows of data that has a drop down for each row to a database table. For example, I have a list of students, and a drop down next to their name that displays the number of minutes they were in class.Example Here
My issue is that I have a foreach loop that is retrieving my students from a stored procedure. I found a few helpful tips on stackoverflow, but nothing that will help with a foreach loop. If I put the "Insert" button inside foreach, then it will work, but there will be a "Insert" button on every row. I just want one "Insert" button, like the example, to input all my drop down selections. Any ideas to point me to the right direction?
Here is the View
<div class="table-responsive">
<div style="margin-top:20px;">
<table id="tablereport" class="table table-striped table-bordered table-hover table-condensed">
<thead style="background-color:black; font-weight:bold; color:aliceblue">
<tr>
</thead>
<thead style="background-color:black; font-weight:bold; color:aliceblue">
<tr>
<th>ClassID</th>
<th>SID</th>
<th>FullName</th>
<th>Minutes</th>
</tr>
</thead>
<tbody>
@{
foreach (System.Data.DataRow dr in ViewBag.general.Rows)
{
<tr>
@using (Html.BeginForm("Index", "Minutes", new { ClassID = dr["ClassID"], SID = dr["SID"], FullName = dr["FullName"] }))
{
<td>@dr["ClassID"]</td>
<td>@dr["SID"]</td>
<td>@dr["FullName"]</td>
<td>
@Html.DropDownList("Minutes", new List<SelectListItem>
{
new SelectListItem{ Text="50", Value = "50" },
new SelectListItem{ Text="40", Value = "40" },
new SelectListItem{ Text="30", Value = "30" },
new SelectListItem{ Text="20", Value = "20" },
new SelectListItem{ Text="10", Value = "10" },
})
</td>
}
</tr>
}
}
</tbody>
</table>
<div>
<input type="submit" id="formsubmit" name="submit" value="Insert" class="btn btn-default">
</div>
</div>
Here is my controller
public class MinutesController : Controller
{
// GET: Minutes
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(ClassModel m)
{
var pClassID = m.ClassID;
var pSID = m.SID;
var pFullName = m.FullName;
var pMinutes = m.Minutes;
new DB().InsertMinutes(m.ClassID, m.Minutes, m.SID, m.FullName);
return RedirectToAction("Index", "Minutes", new {ClassID = pClassID, Minutes = pMinutes, SID = pSID, FullName = pFullName });
}
}
}