C# 复选框MVC-组-用户

C# 复选框MVC-组-用户,c#,asp.net-mvc,checkbox,C#,Asp.net Mvc,Checkbox,我有一个包含表用户和角色的数据库,我需要一个视图来显示所有可用的角色,并检查用户拥有的角色。我已经有了显示所有可用角色的方法和显示用户拥有的角色的方法,但我真的不知道如何在视图中实现它,如何使这些复选框实际处于选中状态。非常感谢你的帮助 表角色 [![在此处输入图像描述][1][1] 用于所有可用角色的方法 [![在此处输入图像描述][2][2] 所选角色的方法 [![在此处输入图像描述][3][3] 模型 公共类UserRoleModel { 公共int ID{get;set;} public

我有一个包含表用户和角色的数据库,我需要一个视图来显示所有可用的角色,并检查用户拥有的角色。我已经有了显示所有可用角色的方法和显示用户拥有的角色的方法,但我真的不知道如何在视图中实现它,如何使这些复选框实际处于选中状态。非常感谢你的帮助

表角色

[![在此处输入图像描述][1][1]

用于所有可用角色的方法

[![在此处输入图像描述][2][2]

所选角色的方法

[![在此处输入图像描述][3][3]

模型

公共类UserRoleModel
{
公共int ID{get;set;}
public int?UserID{get;set;}
公共用户角色模型()
{
ListRole=新列表();
}
公共字符串名称{get;set;}
公共列表ListRole{get;set;}
公共列表用户角色{get;set;}
}
公共类RoleVM
{
公共int ID{get;set;}
公共字符串名称{get;set;}
公共布尔值被选为{get;set;}
}
使用复选框查看

@for(int i = 0; i < Model.ListRole.Count; i++)
{
    @Html.HiddenFor(m => m.ListRole[i].ID)
    @Html.CheckBoxFor(m => m.ListRole[i].IsSelected, new { @checked = "checked" }); 
    @Html.LabelFor(m => m.ListRole[i].IsSelected, Model.ListRole[i].Name)
}
for(int i=0;im.ListRole[i].ID) @CheckBoxFor(m=>m.ListRole[i].IsSelected,new{@checked=“checked”}); @Html.LabelFor(m=>m.ListRole[i].IsSelected,Model.ListRole[i].Name) }
您只需查看列表即可:

@for(int i = 0; i < Model.ListRole.Count; i++)
{
    if(Model.UserRole.Select(x => x.Name).Contains(Model.ListRole[i].Name))
    {
       @Html.HiddenFor(m => m.ListRole[i].ID)
       @Html.CheckBoxFor(m => m.ListRole[i].IsSelected, new { @checked = "checked" }); 
       @Html.LabelFor(m => m.ListRole[i].IsSelected, Model.ListRole[i].Name)
    }
    else
    {
       @Html.HiddenFor(m => m.ListRole[i].ID)
       @Html.CheckBoxFor(m => m.ListRole[i].IsSelected); 
       @Html.LabelFor(m => m.ListRole[i].IsSelected, Model.ListRole[i].Name)
    }
}
for(int i=0;ix.Name).Contains(Model.ListRole[i].Name)) { @Html.HiddenFor(m=>m.ListRole[i].ID) @CheckBoxFor(m=>m.ListRole[i].IsSelected,new{@checked=“checked”}); @Html.LabelFor(m=>m.ListRole[i].IsSelected,Model.ListRole[i].Name) } 其他的 { @Html.HiddenFor(m=>m.ListRole[i].ID) @CheckBoxFor(m=>m.ListRole[i].IsSelected); @Html.LabelFor(m=>m.ListRole[i].IsSelected,Model.ListRole[i].Name) } }
不能说这是最好的方法,但它会起作用的。另一个机会是在右边填写您的型号
IsSelected
字段。

您只需查看列表即可:

@for(int i = 0; i < Model.ListRole.Count; i++)
{
    if(Model.UserRole.Select(x => x.Name).Contains(Model.ListRole[i].Name))
    {
       @Html.HiddenFor(m => m.ListRole[i].ID)
       @Html.CheckBoxFor(m => m.ListRole[i].IsSelected, new { @checked = "checked" }); 
       @Html.LabelFor(m => m.ListRole[i].IsSelected, Model.ListRole[i].Name)
    }
    else
    {
       @Html.HiddenFor(m => m.ListRole[i].ID)
       @Html.CheckBoxFor(m => m.ListRole[i].IsSelected); 
       @Html.LabelFor(m => m.ListRole[i].IsSelected, Model.ListRole[i].Name)
    }
}
for(int i=0;ix.Name).Contains(Model.ListRole[i].Name)) { @Html.HiddenFor(m=>m.ListRole[i].ID) @CheckBoxFor(m=>m.ListRole[i].IsSelected,new{@checked=“checked”}); @Html.LabelFor(m=>m.ListRole[i].IsSelected,Model.ListRole[i].Name) } 其他的 { @Html.HiddenFor(m=>m.ListRole[i].ID) @CheckBoxFor(m=>m.ListRole[i].IsSelected); @Html.LabelFor(m=>m.ListRole[i].IsSelected,Model.ListRole[i].Name) } }
不能说这是最好的方法,但它会起作用的。另一个机会是填写您的model
IsSelected
字段右侧。

在MVC和Razor中,当复选框有值时会选中复选框,因此基本上您必须将正确的值附加到选中的值上

根据文件:

public static MvcHtmlString CheckBoxFor<TModel>(
    this HtmlHelper<TModel> htmlHelper,
    Expression<Func<TModel, bool>> expression
)

其中,
m.ListRole[i]
表示模型中的一些布尔值。

在MVC和Razor中,当复选框有值时,它们会被选中,因此基本上您必须将正确的值附加到被选中的值上

根据文件:

public static MvcHtmlString CheckBoxFor<TModel>(
    this HtmlHelper<TModel> htmlHelper,
    Expression<Func<TModel, bool>> expression
)

其中
m.ListRole[i]
表示模型中的某个布尔值。

您尝试过MVCCheckboxList吗?如果您可以使用此库,将显示以下内容:

@Html.CheckBoxListFor(m => //string array that is bound to your model, will have selected options on POST
    m => //SelectList of all available items
    m => m.Value,
    m => m.Text,
    m => //collection of SelectListItem for all selected items,
    MvcCheckBoxList.Model.Position.Vertical,
    x => new { @class = "editor-cbl" })

你试过MVCCheckboxList吗?如果您可以使用此库,将显示以下内容:

@Html.CheckBoxListFor(m => //string array that is bound to your model, will have selected options on POST
    m => //SelectList of all available items
    m => m.Value,
    m => m.Text,
    m => //collection of SelectListItem for all selected items,
    MvcCheckBoxList.Model.Position.Vertical,
    x => new { @class = "editor-cbl" })

首先,正如我在前面的问题中指出的,您的数据库结构不正确。要正确表示这一点,您需要3个表,
用户
角色
用户角色
,这是
用户
角色
之间的多对多关系,包含
用户ID
(FK到
用户
表)和
角色ID
(FK到
角色
表)的字段

您已表示不准备更改此设置(意味着您甚至不能添加新角色,不能更改现有角色的名称等),但至少您需要所有可用角色的一些表示。使用
Distinct()
查询现有角色无法工作,因为最初数据库将为空。而且您的模型不需要两个
RoleVM
集合(您唯一的编辑集合)

首先需要基于所有角色创建
RoleVM
集合,然后基于用户的当前角色设置
IsSelected
属性

您的GET方法需要类似于

public ActionResult Edit(int ID)
{
  // Get all roles
  IEnumerable<string> roles = new List<string>(){ "Administrator", "Gestor", "Supervisor", "SecurityManager", "Admin" };
  // Get the User
  User user = db.Users().Where(u => u.ID == ID).FirstOrDefault();
  // Get the user roles (if not included in User model
  IEnumerable<UserRole> userRoles = db.UserRoles().Where(r => r.UserID == ID);
  // Create collection of all roles
  List<RoleVM> allRoles = roles.Select(r => new RoleVM()
  {
    Name = r
  }).ToList();

  // Set the ID and IsSelected properties based on the user roles
  foreach(RoleVM role in allRoles)
  {
    UserRole userRole = userRoles.FirstOrDefault(r => r.Role == role.Name);
    if (userRole != null)
    {
      role.ID = userRole.ID;
      role.IsSelected = true;
    }
  }

  // Initialize the view model
  UserVM model = new UserVM()
  {
    ID = user.ID,
    Name = user.Name,
    Roles = allRoles
  };
  return View(model);
}
public ActionResult编辑(int-ID)
{
//获得所有角色
IEnumerable roles=新列表(){“管理员”、“管理者”、“主管”、“安全管理者”、“管理员”};
//获取用户
User User=db.Users()。其中(u=>u.ID==ID).FirstOrDefault();
//获取用户角色(如果未包含在用户模型中)
IEnumerable userRoles=db.userRoles()。其中(r=>r.UserID==ID);
//创建所有角色的集合
列出allRoles=roles.Select(r=>newrolevm()
{
Name=r
}).ToList();
//根据用户角色设置ID和IsSelected属性
foreach(所有角色中的RoleVM角色)
{
UserRole UserRole=userRoles.FirstOrDefault(r=>r.Role==Role.Name);
if(userRole!=null)
{
role.ID=userRole.ID;
role.IsSelected=true;
}
}
//初始化视图模型
UserVM model=newuservm()
{
ID=user.ID,
Name=user.Name,
角色=所有角色
};
返回视图(模型);
}

在视图中,从
CheckBoxFor()中删除
new{@checked=“checked”}
method

首先,正如我在前面的问题中指出的,您的数据库结构不正确。要正确表示这一点,您需要3个表,
用户
角色
用户角色
,这是
用户之间的多对多关系