MVC CheckBoxList模型绑定非布尔值

我想将List绑定到CheckBox并获取所选值。 我需要显示两个这样的Checkbox表,并且必须检索这两个ID。

下面是我的ViewModel

public partial class ViewModel_Role_Security { public List oRoleMaster { get; set; } public List oSecurityMaster { get; set; } } 

所有这三个都有这两个值1. ID 2.名称(在这种情况下为Role-ID,RoleName | for Securities – ID,SecurityName …)//添加bool类型的第3个属性被选中以便仅使用eith复选框那么你会得到它回来这些没有任何布尔

通过使用此ViewModel,我使用以下方法绑定这些项目…

 public ActionResult AddingRoleSecurity() { ListRoles = new List(); ListSecurities = new List(); //And then populate them with data ... var model = new ViewModel_Role_Security(); model.oRoleMaster = ListRoles; model.oSecurityMaster = ListSecurities; return View(model); } 

我对应的cshtml文件是..

 @model KnackWFM.BusinessEntities.ViewModel_Role_Security @using (Html.BeginForm()) { 

User Role

@for (int i = 0; i < Model.oRoleMaster.Count; i++) {
@Html.CheckBoxFor(Model.oRoleMaster[i].RoleID.selec) }

Role Security

@for (int i = 0; i < Model.oSecurityMaster.Count; i++) {
}
}

我得到以下输出, 在此处输入图像描述

我想将选中的值作为模型。

我有这样的HttpPost方法,但它返回空值。

 [HttpPost] public ActionResult AddingRoleSecurity(ViewModel_Role_Security model) { return View(); } 

请告诉我如何获取模型中的已检入值?

非常感谢你!

只是为了充实我上面的评论……

对于checkboxList – viewmodel应包含标识符属性和布尔选择属性。 如果它尚未扩展或创建新的ViewModel类来实现此目的 – 将现有模型映射到此特定视图模型。

即 – 您的模型类

 public class UserRole { public int RoleID {get; set;} public string RoleName {get; set;} public bool Selected {get; set;} } public class UserSecurity { public int SecurityID {get; set;} public string SecurityName {get; set;} public bool Selected {get; set;} } public class UserRoleAndSecurityModel { public List RoleMaster {get; set;} public List SecurityMaster {get; set;} } 

您的视图:请注意,除了复选框Html.HiddenFor() ,每个UserRole / UserSecurity ID属性都包含Html.HiddenFor() ,这允许MVC在回发后绑定ID属性。

 @model UserRoleAndSecurityModel @using (Html.BeginForm()) { 

User Role

@for (int i = 0; i < Model.RoleMaster.Count; i++) { @Html.CheckBoxFor(m => m.RoleMaster[i].Selected) @Html.HiddenFor(m => m.RoleMaster[i].RoleId) @Html.LabelFor(m => m.RoleMaster[i].Selected, Model.RoleMaster[i].RoleName)
}

Role Security

@for (int i = 0; i < Model.SecurityMaster.Count; i++) { @Html.CheckBoxFor(m => m.SecurityMaster[i].Selected) @Html.HiddenFor(m => m.SecurityMaster[i].SecurityId) @Html.LabelFor(m => m.SecurityMaster[i].Selected, Model.SecurityMaster[i].SecurityName)
}

你的控制器现在也应该使用上面的新型号!

我不希望看起来像是詹姆斯出色的工作,但我能做的就是新答案。 由于我不理解的原因,我的编辑纠正了代码中的编译错误被拒绝,因为没有纠正关键问题。 所以我发布这个作为一个完整的工作答案。

型号:

 public class UserRole { public int RoleID {get; set;} public string RoleName {get; set;} public bool Selected {get; set;} } public class UserSecurity { public int SecurityID {get; set;} public string SecurityName {get; set;} public bool Selected {get; set;} } public class UserRoleAndSecurityModel { public List RoleMaster {get; set;} public List SecurityMaster {get; set;} } 

并且观点:

 @model UserRoleAndSecurityModel @using (Html.BeginForm()) { 

User Role

@for (int i = 0; i < Model.RoleMaster.Count; i++) { @Html.CheckBoxFor(m => m.RoleMaster[i].Selected) @Html.HiddenFor(m => m.RoleMaster[i].RoleId) @Html.LabelFor(m => m.RoleMaster[i].Selected, Model.RoleMaster[i].RoleName)
}

Role Security

@for (int i = 0; i < Model.SecurityMaster.Count; i++) { @Html.CheckBoxFor(m => m.SecurityMaster[i].Selected) @Html.HiddenFor(m => m.SecurityMaster[i].SecurityId) @Html.LabelFor(m => m.SecurityMaster[i].Selected, Model.SecurityMaster[i].SecurityName)
}

我会使用https://www.nuget.org/packages/BeginCollectionItem/

这将帮助您使用GUID获得正确的绑定序列

最好的问候Burim

你应该为你的两个模型添加第三个bool属性,

  public bool IsSelected { get; set; } 

并将其默认设置为false,并在检查相应的复选框时将其发回,并根据真实值,您可以使用相应的选定值和文本执行任何操作

这是我的代码,我已经使用了一些在哪里实现相同的:

   @{int i = 0; } @foreach (var item in Model.SurfaceTypeList) {    @Html.Label(item.Text) 

i++; }

model包含一个属性:

 public List SurfaceTypeList { get; set; } public class SurfaceType { public bool IsSelected { get; set; } public string Text { get; set; } public string Value { get; set; } } 

这是我如何检索文本/值:

  foreach (var item in modelData.SurfaceTypeList) { if (item.IsSelected) { var surfaceType = new XElement("SurfaceType"); var id = new XElement("Id"); id.Add(item.Value); surfaceType.Add(id); var i = id.Value; surfaceTypeList.Add(surfaceType); } }