I want to bind a List to CheckBox and get the selected values. I need to display two such Checkbox tables and have to retrieve both the IDs.
Below is my ViewModel
public partial class ViewModel_Role_Security
{
public List<Roles> oRoleMaster { get; set; }
public List<Securities> oSecurityMaster { get; set; }
}
All these three have these two values 1. ID 2. Name (In this case for Role - ID, RoleName | for Securities - ID, SecurityName ...) //add 3rd property of type bool isselected in order to work eith checkboxes only then you will get it posted back These don't have any boolean values
By using this ViewModel I'm binding these items using the below method...
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);
}
My corresponding cshtml file is..
@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>
}
For which I get the following output,
I would want to get the checked values as a model.
I have a HttpPost method like this, but it returns null values.
[HttpPost]
public ActionResult AddingRoleSecurity(ViewModel_Role_Security model)
{
return View();
}
Please let me know how do I get the checked in values in the model?
Thank you very much!
Roles
andSecurities
that includes a propertybool IsSelected
so you can correctly use a strongly typed helpers in collections – Idleman@Html.CheckBoxFor(m => m.oRoleMaster[i].RoleID.selec)
Then it'll figure out the (important) correct name for the checkbox (which if you look at the rendered HTML should beoRoleMaster[1].RoleID.selec
) – Picayune@HiddenFor(m => m.oRoleMaster[i].RoleID.ID)
next to every checkboxfor to get the ID posted back – Picayunebool IsSelected
properties so you can do this properly. It would take you far less time than writing this question and you would not have any problems. – Idleman