MVC 2 - 无法检索MVC Contrib网格中的复选框值

时间:2011-09-30 14:57:49

标签: asp.net-mvc mvccontrib

我的MVC Contrib网格上有一个复选框;

        <%= Html.Grid(Model.TeamMembers).Columns(column =>
            {
                column.For(model => model.Surname);
                column.For(model => model.Forename);
                column.For(model => model.DaysLeftThisYear).Named("Days Left This Year");
                column.For(model => model.DaysLeftNextYear).Named("Days Left Next Year");
                column.For(model => Html.CheckBox("chkbox", model.SelectForConfirmationFlag))
                    .Named("Select?")
                    .Sortable(false);
            }).Sort((GridSortOptions)ViewData["sort"]) %>
    </div>
        <p><%= Html.Pager((IPagination)Model.TeamMembers)%></p>

然后,用户可以单击复选框,但是当他提交时,新值不会在控制器中被选中。 该页面包含在具有BeginForm语句的页面中;

<% using (Html.BeginForm("Home", "Approver", FormMethod.Post, new { id = "frmHome" }))
   {%>

我的控制器看起来像;

[HttpGet]
[Authorize(Roles = "Administrator, ManagerIT, ManagerAccounts, Approver")]
public ActionResult Home(GridSortOptions sort, int? page)
{
    SessionObjects.LoggedInUserName = User.Identity.Name;
    if (SessionObjects.ApprovalViewModel.TeamMembers.Count() == 0)
        SessionObjects.ApprovalViewModel = new ApprovalViewModel();
    TempData["ReadOnly"] = true;
    if (sort.Column != null)
    {
        SessionObjects.ApprovalViewModel.TeamMembers = SessionObjects.ApprovalViewModel.TeamMembers.OrderBy(sort.Column, sort.Direction);
    }
    SessionObjects.ApprovalViewModel.TeamMembers = 
        SessionObjects.ApprovalViewModel.TeamMembers.AsPagination(page ?? 1, Utility.GetPageLength());
    ViewData["sort"] = sort;
    return View(SessionObjects.ApprovalViewModel);
}

[HttpPost]
public ActionResult Home(ApprovalViewModel avm)
{
    if (avm.TeamMembers.Count() == 0)
    {
        TempData["ErrorMessage"] = "You have no team members to select";
        return RedirectToAction("Home");
    }
    else if (avm.TeamMembers.Where(x => x.SelectForConfirmationFlag == true).Count() == 0)
    {
        TempData["ErrorMessage"] = "You must select at least one team member";
        return RedirectToAction("Home");
    }
    else
    {
        string[] selectedEmployees = avm.TeamMembers
                                    .Where(x => x.SelectForConfirmationFlag == true)
                                    .Select(x => x.EmployeeId.ToString()).ToArray();
        var result = RedirectToAction("ConfirmBookings", "Approver",
                                        new { selectedEmployeesParameters = selectedEmployees });
        result.AddArraysToRouteValues();
        return result;
    }
}

2 个答案:

答案 0 :(得分:2)

您是否应该匹配复选框的name属性?您的以下行不会生成名为SelectForConfirmationFlag

的复选框
column.For(model => Html.CheckBox("chkbox", model.SelectForConfirmationFlag))

你能尝试一下吗?

column.For(model => Html.CheckBoxFor(m => m.SelectForConfirmationFlag))

或者

column.For(model => Html.CheckBox("SelectForConfirmationFlag",model.SelectForConfirmationFlag))

答案 1 :(得分:0)

我不熟悉MVC Contrib网格,但我的方法是将团队成员ID添加为复选框的值,并在控制器方法中使用一组id。像这样:

column.For(model => Html.CheckBox("teammembers", model.SelectForConfirmationFlag, new { value = model.Id }))
                    .Named("Select?")
                    .Sortable(false);

控制器方法:

[HttpPost]
public ActionResult Home(int[] teammembers)
{
    // code to process ids...
}