来自数据库的 ASP.Net MVC 3 CheckBoxList

发布于 2024-12-06 14:37:43 字数 770 浏览 0 评论 0原文

我有一个名为的表

tb_role
id  role
1   admin
2   user
3   viewer

,视图如下:

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

我想问,如何从表中列出我的复选框(CBRole)?所以我的 CBRole 从我的表中列出。

非常感谢

编辑

假设我有这样的角色表:

tb_role
RoleId  Role_Name
     1  SalesCreate
     2  SalesEdit
     3  AgentCreate
     4  AgentEdit

我想在复选框中列出销售角色(SalesCreate 和 SalesEdit,所以它只有 2 个复选框),该怎么做? 谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

Smile简单爱 2024-12-13 14:37:43

从您的控制器中,您可以使用以下属性填充您的视图模型:

您的 RoleViewModel

public IList<int> RolesSelected { get; set; }
public MultiSelectList Roles { get; set; }

从处理 Get 调用的控制器(例如 /roles/edit/1 )

 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);

执行类似的操作

 @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>
 }

然后在您的视图中(在表单标记内),您将在控制器中 回答 Post 部分,您将访问 RolesSelected 并检查 ID。

在示例中,我放置了一个 div,但您显然可以将其更改为您喜欢的内容。希望有帮助

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

缱倦旧时光 2024-12-13 14:37:43

您可能想要类似于 StackOverflow 上的以下帖子的内容,Enum to CheckBox

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文