开发者

CheckBoxList ASP.Net MVC 3 from database

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). I made my tb_role using aspnet configuration, so it doesn't use entities.

here my Controller:

RegisterModel account = new RegisterModel();
account.Roles = new MultiSelectList(Roles.GetAllRoles()); 

and my View:

 <td><select id="Roles" name="Roles">
                    <option>Sales</option>
                    <option>Agent</option>
     </select>
 </td>

@foreach (var item in Model.Roles)
             {
                 <label for="@item.Value">
                   <input type="checkbox" id="@item.Value" name="RolesSelected" value="@item.Value" @(item.Selected ? "checked" : "") />@item.Text</label>
             }

开发者_JAVA技巧when i run my project, my checkbox list all of the roles in tb_role. I want that if I choose Sales, my checkbox list all the Roles for Sales (SalesCreate and SalesEdit). how to do that ?

thanks a lot


Couple of ways to do this. One way is this:

Surround the <select> with a <form> tag and do a submit on change.

in your controller:

public ActionResult Index(..., string role)
{
    //... rest of your code
    RegisterModel account = new RegisterModel();
    account.Roles = new MultiSelectList(Roles.GetAllRoles().Where(w => w.StartsWith(role)); 
    //... rest of your code
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜