ASP.Net MVC 3 CheckBoxList from database
i have a table called
tb_role
id role
1 admin
2 user
3 viewer
and for the View is like this :
<div style="width:50%; float:right;">
<legend>User Role</legend>
<table>
<tr>
<th>Role</th>
</tr>
<tr>
<td align="center">&开发者_Python百科lt;input type="checkbox" id="CBRole"/></td>
</tr>
</table>
</div>
I want to ask, how to list my checkbox(CBRole) from my table? so my CBRole is listed from my table.
thanks a lot
EDIT
assumed that i have Roles table like this :
tb_role
RoleId Role_Name
1 SalesCreate
2 SalesEdit
3 AgentCreate
4 AgentEdit
i want to list role for Sales in checkbox (SalesCreate and SalesEdit, so its only have 2 checboxes), how to do that ? thanks
From your controller you populate your view model with these properties:
Your RoleViewModel
public IList<int> RolesSelected { get; set; }
public MultiSelectList Roles { get; set; }
From controller that handles the Get call (/roles/edit/1 for example)
model.RolesSelected = new List<int>();
//here the code to populate the eventually already selected roles (update case)
model.Roles = new MultiSelectList(repository.GetRoles(), "Id", "Name", model.SettoriSelected);
then in your view (inside a form tag) you will do something like this
@foreach (var item in Model.Roles)
{
<div class="MyClass">
<label for="@item.Value" class="MyClassForCheck">
<input type="checkbox" id="@item.Value" name="RolesSelected" value="@item.Value" @(item.Selected ? "checked" : "") />@item.Text</label>
</div>
}
in the controller that answer the Post part you will access the RolesSelected with the IDs checked
In the example I have put a div, but yuo can change it to what you like obviously. Hope it helps
You probably want to have something that looks like the following post on StackOverflow, Enum to CheckBox
精彩评论