1

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"><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

ntep vodka
  • 735
  • 3
  • 11
  • 27

2 Answers2

0

You probably want to have something that looks like the following post on StackOverflow, Enum to CheckBox

Community
  • 1
  • 1
Pieter Germishuys
  • 4,828
  • 1
  • 26
  • 34
0

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

Iridio
  • 9,213
  • 4
  • 49
  • 71
  • thanks Iridio for reply. i want to ask how to make GetRoles()? because i dont use entities for Roles. – ntep vodka Sep 26 '11 at 06:14
  • I supposed you use a repository pattern. Simply replace the "repository.GetRoles()" with your method to get the roles list. If you mean your are looking for infos on how to use entities I suggest you to take a look at NerdDinner sample. Maybe it can give you some ideas – Iridio Sep 26 '11 at 06:23
  • thanks Iridigo, i remember that i use membership for user account. and in membership, there a method for GetAllRoles. can I list GetAllRoles to my CheckBox ? – ntep vodka Sep 26 '11 at 06:28
  • Yep. What it matters is that you obtain a list of the class you need – Iridio Sep 26 '11 at 07:56