【发布时间】:2018-09-27 00:30:32
【问题描述】:
自从我写 razor 已经有一段时间了,我一直坚持从列表中创建 checkboxfor 选项并使其工作。我尝试了其他一些答案,但到目前为止没有一个为我解决这个问题。
这是我在视图中使用的模型。我正在尝试为角色创建复选框。
public class AccountRegisterModel
{
public string firstName { get; set; }
public string lastName { get; set; }
public string emailAddresses { get; set; }
public string password { get; set; }
public string passwordConfirm { get; set; }
public bool? isactive { get; set; }
public string phone { get; set; }
public string subscriptionKey { get; set; }
public string SalesPersonReference { get; set; }
public int ProjectId { get; set; }
public int SalesPersonId { get; set; }
public int? SalesRegionId { get; set; }
public List<SelectListModel> Roles { get; set; }
public List<SelectListModel> Projects { get; set; }
public List<SelectListModel> SalesRegions { get; set; }
public int TenantID { get; set; }
public Guid TenantGuid { get; set; }
public string UserID { get; set; }
public string UserEmail { get; set; }
}
角色类型,SelectListModel
public class SelectListModel
{
public string Key { get; set; }
public string Value { get; set; }
public bool Selected { get; set; }
public string ProjectHierarchyCode { get; set; }
public string SalesRegionReference { get; set; }
public int? Depth { get; set; }
public bool isSelected { get; set; }
}
我尝试创建复选框的方式
@foreach (var item in Model.Roles)
{
@Html.CheckBoxFor(m=> item.isSelected, new { value = item.Value, id= "chkRole_"+item.Value})
}
模型“角色”列表被填充到控制器中,我在页面上看到了 9 个未命名的复选框。命名它们可以通过标签来解决,所以这不是问题。
问题是,当我选中某些框并点击“保存”时,选择的角色没有发布到控制器。模型参数中的角色字段来自null。所以我认为我有一个绑定问题。
【问题讨论】:
-
你试过这种方式吗?
@Html.CheckBoxFor(m => item.isSelected, new { value = item.Value, id= "chkRole_"+item.Value, Name = "CheckBox"}) -
刚刚试了一下,Roles 还是空的。