开发者

CheckBoxList ASP.Net MVC 3 from database

开发者 https://www.devze.com 2023-04-07 06:17 出处:网络
assumed that i have Roles table like this : tb_role RoleIdRole_Name 1SalesCreate 2SalesEdit 3AgentCreate 4AgentEdit

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

精彩评论

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

关注公众号