检索ASP .NET MVC 5中的Checked Checkbox项目列表?

时间:2015-03-18 00:14:11

标签: c# asp.net asp.net-mvc entity-framework

遵循EF Contoso大学教程入门: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 我正在尝试在学生/索引页面上为每个学生添加一个复选框。但是,如何在StudentController / Index()Action中检索已检查的Student实体列表?

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

1 个答案:

答案 0 :(得分:0)

而不是

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

成功

@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.
        }            
}

但是您必须为SelectStudent属性创建一个模型,或者将其包含在您已在该Index视图中的Model中。它只是寻找某种独特的标识符,计数器为它提供了它所需要的东西,让它知道到达控制器时是一个列表。

如果你不想使用ASP.NET MVC Razor语法发布一个东西列表,你可以在javascript中进行操作。

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

您可能需要某种标识符来查看选择了哪些学生。否则,您将获得一个真/假值列表,而不知道它们与谁有什么关系。 (例如:用户ID 5,真)