【问题标题】:Retrieve a list of Checked Checkbox items in ASP .NET MVC 5?检索 ASP .NET MVC 5 中的 Checked Checkbox 项目列表?
【发布时间】:2015-03-18 00:14:11
【问题描述】:

遵循 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() 操作中检索已检查的学生实体列表?

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

【问题讨论】:

  • 您的 POST 方法参数需要为 IEnumerable&lt;Student&gt;。您需要为所有学生准备一份表格(不是每个学生一份)。 Student 需要一个属性 bool IsSelected 或类似的属性来绑定复选框。并且循环需要是for 循环(不是foreach

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


【解决方案1】:

而不是

@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 属性创建一个模型,或者将其包含在您已经在该索引视图中的模型中。它只是在寻找某种唯一标识符,然后计数器为它提供所需的信息,并在到达控制器时让它知道这是一个列表。

如果您不想使用 ASP.NET MVC Razor 语法来发布内容列表,您可以在 javascript 中完成。

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

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

【讨论】:

  • Stephen 指出,您不希望每个学生都有单独的表格。
猜你喜欢
  • 1970-01-01
  • 2014-04-26
  • 2016-12-16
  • 1970-01-01
  • 2011-09-23
  • 1970-01-01
  • 2016-01-02
  • 1970-01-01
  • 2010-11-30
相关资源
最近更新 更多