作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的 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>
然后用户可以单击复选框,但是当他提交时, Controller 中不会选取新值。该页面包含在具有 BeginForm 语句的页面中;
<% using (Html.BeginForm("Home", "Approver", FormMethod.Post, new { id = "frmHome" }))
{%>
我的 Controller 看起来像;
[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;
}
}
最佳答案
您不应该匹配复选框的名称属性吗?您的下一行不会生成名称为 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))
关于asp.net-mvc - MVC 2 - 无法检索 MVCContrib 网格中的复选框值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7612254/
我是一名优秀的程序员,十分优秀!