我想将List绑定到CheckBox并获取所选值.我需要显示两个这样的CheckBox表,并且必须检索这两个ID.
下面是我的viewmodel
public partial class viewmodel_Role_Security { public List<Roles> oRoleMaster { get; set; } public List<Securities> oSecurityMaster { get; set; } }
所有这三个都有这两个值
身份证
2.名称(在本例中为Role-ID,RoleName | for Securities – ID,SecurityName ……)
//添加bool isselected类型的第3个属性,以便只使用复选框,然后才能将其发回
这些没有任何布尔值
public ActionResult AddingRoleSecurity() { ListRoles = new List<Roles>(); ListSecurities = new List<Securities>(); //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()) { <div class="contentsecurity"> <div class="User_role"> <p class="Security_role">User Role</p> @for (int i = 0; i < Model.oRoleMaster.Count; i++) { <input id="@Model.oRoleMaster[i].RoleID" name="@Model.oRoleMaster[i].RoleName" type="checkBox" value="@(Model.oRoleMaster[i].RoleName)" /> <label for="@Model.oRoleMaster[i].RoleID">@Model.oRoleMaster[i].RoleName</label> <br /> @Html.CheckBoxFor(Model.oRoleMaster[i].RoleID.selec) } </div> <div class="User_Page"> <p class="Security_role">Role Security</p> @for (int i = 0; i < Model.oSecurityMaster.Count; i++) { <input id="@Model.oSecurityMaster[i].SecurityID" name="@Model.oSecurityMaster[i].SecurityName" type="checkBox" value="@(Model.oSecurityMaster[i].SecurityName)" /> <label for="@Model.oSecurityMaster[i].SecurityID">@Model.oSecurityMaster[i].SecurityName</label> <br /> } </div> <div class="bottombuttonsecurity"> <button type="submit" id="btnSave" name="Command" value="Save" style="background-color: #3d3c4c;border-radius: 8px;color: white;padding: 5px;border: 1px solid #3d3c4c;">Save</button> </div> </div> }
我得到以下输出,
我想将选中的值作为模型.
[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<UserRole> RoleMaster {get; set;} public List<UserSecurity> SecurityMaster {get; set;} }
你的观点:
请注意,除了复选框之外,每个UserRole / UserSecurity ID属性都包含Html.HiddenFor(),这允许MVC在回发后绑定ID属性.
@model UserRoleAndSecurityModel @using (Html.BeginForm()) { <div class="contentsecurity"> <div class="User_role"> <p class="Security_role">User Role</p> @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) <br /> } </div> <div class="User_Page"> <p class="Security_role">Role Security</p> @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) <br /> } </div> <div class="bottombuttonsecurity"> <button type="submit" id="btnSave" name="Command" value="Save" style="background-color: #3d3c4c;border-radius: 8px;color: white;padding: 5px;border: 1px solid #3d3c4c;">Save</button> </div> </div>
你的控制器现在也应该使用上面的新型号了!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。