У меня есть флажок в моей сетке 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;
}
}