简体   繁体   中英

Retrieve a list of Checked Checkbox items in ASP .NET MVC 5?

Following the Getting Started With EF Contoso University Tutorial: http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application I'm attempting to add a checkbox for each Student on the Students/Index page. But how do you retrieve the list of checked Student entities in the StudentController/Index() Action?

Views/Student/Index.cshtml:

@model IEnumerable<ContosoUniversity.Models.Student>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>
@using (Html.BeginForm())
{
    <p>
        Find by name: @Html.TextBox("SearchString")
        <input type="submit" value="Search" />
    </p>
}
<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>
            Selected
        </th>
        <th>
            @Html.ActionLink("Last Name", "Index", new { SortOrder = ViewBag.NewLastNameSortParm })
        </th>
        <th>
            @Html.ActionLink("First Name", "Index", new { SortOrder = ViewBag.NewFirstNameSortParm })
        </th>
        <th>
            @Html.ActionLink("Enrollment Date", "Index", new { SortOrder = ViewBag.NewDateSortParm })
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        @using (Html.BeginForm())
        {
            <p>
                @Html.CheckBox("SelectStudent", false)
                //Is this the correct format to instantiate a CheckBox?
            </p>
        }
        <td>
            @Html.DisplayFor(modelItem => item.LastName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.FirstMidName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.EnrollmentDate)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
            @Html.ActionLink("Details", "Details", new { id=item.ID }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.ID })
        </td>
    </tr>
}

</table>

Controllers/StudentController.cs:
namespace ContosoUniversity.Controllers
{
public class StudentController : Controller
{
    private SchoolContext db = new SchoolContext();

    // GET: Student
    public ActionResult Index(bool SelectStudent)
    {
       //How do I retrieve list of Selected Student Entities??
       return View(students.ToList());
    }
 }
}

instead of the

@foreach (var item in Model) {
        @using (Html.BeginForm())
        {
            <p>
                @Html.CheckBox("SelectStudent", false)
                //Is this the correct format to instantiate a CheckBox?
            </p>
        }            
}

Make it

@for (var i = 0; i < Model.Count(); i++) {
        @using (Html.BeginForm())
        {
                @Html.CheckBoxFor(model => modelItem[i].SelectStudent)
                //not in my dev environment at the moment so I could have a small syntax error but this is the general premise.
        }            
}

But you will have to make a model for the SelectStudent property or include it in your Model that you have in that Index view already. It is just looking for some sort of unique identifier and the counter gives it what it needs and lets it know that is a list when reaching the controller.

if you don't want to go the route of using the ASP.NET MVC Razor syntax for posting a list of stuff you can just do it in javascript.

 //on some button click
 //get all SelectStudent checkboxes
 //post whichever ones you want to the server.

You are probably going to need some sort of identifier to see which students were selected though. Otherwise you are just going to get a list of true/false values and not know who/what they pertain to. (ex: user id 5, true)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM