0

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 });


    }
  }
}
DilbarC
  • 41
  • 9
  • You have one form, and one submit button, and generate the collection using a `for` loop of `EditorTemplate` (refer [this answer](http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable/30094943#30094943)) –  May 09 '18 at 22:54
  • @StephenMuecke - Thanks for the response, I have been trying to do this, but for some reason my dropdown will not save the selection to my stored procedure. If it helps, I am getting my ClassID, SID, and FullName from another stored procedure. Thanks for the suggestions!! – DilbarC May 14 '18 at 21:12
  • Did you read the link I gave your - you cannot use a `foreach` loop –  May 14 '18 at 22:06
  • @StephenMuecke - whoops, I forgot to mention that i tried the for loop and not the foreach. I am new to MVC and programming in general, do i use the for loop in my view only or do i have to add it to my controller? – DilbarC May 14 '18 at 22:25
  • Then your code is clearly wrong, but I am not psychic and cannot guess what it is. Suggest you ask a new question, or rewrite this one. –  May 14 '18 at 22:28

0 Answers0