0

I am trying to add a checkbox so I can selects a row from my list and from there use that row to generate a letter. I was wondering how I would be able to add a checkbox and bind it to the data I have tried a few ways using something similar to:

checkbox(m => m[i].Lesson)

However it would never accept the i in brackets and come up with an error. below is the layout of my index for the lessons model currently.

Here is the Lessons Index:

@model IEnumerable<FinalMusicApp.Models.Lesson>

@{
    ViewBag.Title = "Index";
}


<h2>Lessons</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>

@using (Html.BeginForm("Index", "Students", FormMethod.Get))
{
    //Search options
    <b>Search Options: </b> @Html.RadioButton("Option", "Instrument")<text>Instrument</text>@Html.RadioButton("Option", "Tutor")<text>Tutor</text>@Html.RadioButton("Option", "Term")<text>Term</text>@Html.TextBox("Search")
    <input type="submit" name="submit" value="Search" />
}



<form method="post">
    <table class="table">
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.IsChecked)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.LessonDate)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.LessonTime)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Duration.TimeTaken)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Instrument.InstrumentName)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Letter.Paid)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Student.FullName)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Tutor.TutorName)
            </th>
            <th>
                CRUD
            </th>
            <th>
                Make a letter
            </th>
        </tr>


        @foreach (var item in Model)

        {
            <tr>
                <td>
                    @Html.CheckBoxFor(modelItem => item.IsChecked)
                </td>

                <td>
                    @Html.DisplayFor(modelItem => item.LessonDate)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.LessonTime)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Duration.TimeTaken)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Instrument.InstrumentName)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Letter.Paid)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Student.FullName)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Tutor.TutorName)
                </td>
                <td>
                    @Html.ActionLink("Edit", "Edit", new { id = item.LessonId }) |
                    @Html.ActionLink("Details", "Details", new { id = item.LessonId }) |
                    @Html.ActionLink("Delete", "Delete", new { id = item.LessonId })

                </td>
                <td>
                    @Html.ActionLink("Generate Letter", "Create", "Letters")
                </td>


            </tr>
        }

    </table>

</form>
Qing Guo
  • 6,041
  • 1
  • 2
  • 10
MarcusLee
  • 27
  • 4

1 Answers1

0

I am trying to add a checkbox so I can selects a row from my list

Do you want something like below?

In the view add a checkbox, for example I use the property CourseName in my model instead, you can use your property:

 @Html.CheckBox("CourseName", new { value = item.CourseName })
 @Html.DisplayFor(modelItem => item.CourseName)

Then in the controller, we can get CourseName value, and get other property value accord to the CourseName, but we need load the list again, we cannot send the full list from view to controller :

var courserow = model.FirstOrDefault(m => m.CourseName == CourseName);

result:

enter image description here

Qing Guo
  • 6,041
  • 1
  • 2
  • 10