arame3333
arame3333

Reputation: 10193

MVC 2 - Cannot retieve checkbox values in MVC Contrib grid

I have a checkbox on my MVC Contrib grid;

        <%= 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>

The user can then click on the checkboxes, but when he submits, the new values are NOT picked up in the controller. The page is contained in a page which has a BeginForm statement;

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

My controller looks like;

[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;
    }
}

Upvotes: 3

Views: 843

Answers (2)

Trax72
Trax72

Reputation: 958

I'm not familiar with MVC Contrib grid, but my approach would be to add the team member ids as value of the checkboxes and work with an array of ids in the controller method. Like so:

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

Controller method:

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

Upvotes: 0

Suhas
Suhas

Reputation: 8458

Are you not supposed to match the name attribute of the checkbox? Your following line does not generate checkbox with name SelectForConfirmationFlag

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

Could you try following instead?

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

Or

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

Upvotes: 2

Related Questions