Asp.net mvc 将所有角色从db显示到dropdownlist

Asp.net mvc 将所有角色从db显示到dropdownlist,asp.net-mvc,html.dropdownlistfor,Asp.net Mvc,Html.dropdownlistfor,我想在dropdownlist中显示db中的所有角色。我已经用这种方式覆盖了角色提供者的GetAllUser方法 public string[] GetAllRoles() { string[] roles = Session.Query<RoleManager>() .Select(r => r.roleName) .ToArray();

我想在dropdownlist中显示db中的所有角色。我已经用这种方式覆盖了角色提供者的GetAllUser方法

       public string[] GetAllRoles()
         {
        string[] roles = Session.Query<RoleManager>()
                       .Select(r => r.roleName)
                       .ToArray();
        return roles;
         }
视图:


现在我的问题是如何从角色的字符串数组中填充dropdownlist。请为我的案例编写示例代码。

您可以使用SelectListItems。只需将所有角色填充到视图模型中即可

 public class RoleViewModel
 {
     public IEnumerable<SelectListItem> RolesList { get; set; }
 }

 public ActionResult DisplayAllRoles()
 {
        var roleModel = new RoleViewModel();

        //string[] allRoles = ((CustomRoleProvider)Roles.Provider).GetAllRoles(); 
        var allRoles = new[] { "role1", "role2" }; //hard coded roles for to demo the above method.

        if (allRoles != null)
        {
            //bind dropDownlist list in view with all roles ???
            roleModel.RolesList = allRoles.Select(x => new SelectListItem() {Text = x, Value = x});
        }
        else
            ModelState.AddModelError("", "No role exists.");

        return View(roleModel);
 }
更新以演示所选值:

在RoleViewModel中,添加其他属性以获取选定值

  public class RoleViewModel
  {
     public RoleViewModel()
     {}

     public string SelectedRole { get; set; }

     public IEnumerable<SelectListItem> RolesList { get; set; }
  }

上述selectedValue就是您选择的值。

您可以使用SelectListItems。只需将所有角色填充到视图模型中即可

 public class RoleViewModel
 {
     public IEnumerable<SelectListItem> RolesList { get; set; }
 }

 public ActionResult DisplayAllRoles()
 {
        var roleModel = new RoleViewModel();

        //string[] allRoles = ((CustomRoleProvider)Roles.Provider).GetAllRoles(); 
        var allRoles = new[] { "role1", "role2" }; //hard coded roles for to demo the above method.

        if (allRoles != null)
        {
            //bind dropDownlist list in view with all roles ???
            roleModel.RolesList = allRoles.Select(x => new SelectListItem() {Text = x, Value = x});
        }
        else
            ModelState.AddModelError("", "No role exists.");

        return View(roleModel);
 }
更新以演示所选值:

在RoleViewModel中,添加其他属性以获取选定值

  public class RoleViewModel
  {
     public RoleViewModel()
     {}

     public string SelectedRole { get; set; }

     public IEnumerable<SelectListItem> RolesList { get; set; }
  }

上述selectedValue就是您选择的值。

我不想硬编码角色值。在我的示例中,字符串数组allroles包含我要填充dropdownlist的角色。当然。在您的示例中,硬编码的值将替换为实际的实现字符串[]allRoles=((CustomRoleProvider)Roles.Provider);这就是我注释掉这一行的原因。var roleModel的作用域在if语句中,但我们在if之外重新使用它。如何使它在if之外可访问。@Wasfa-很抱歉,我刚刚更新了代码,以便在if之外可访问它。另外,我认为您不需要ModelState.AddModelError(“所有角色列表”);主要是因为在返回所有角色时,您不希望模型处于错误状态。谢谢Raj。我还希望在控制器的http post方法中访问dropdownlist的选定值。请您指导我如何操作?我不想硬编码角色值。在我的情况下,字符串数组allroles包含我想要访问的角色填充一个dropdownlist。当然。在您的示例中,硬编码的值将替换为实际的实现字符串[]allRoles=((CustomRoleProvider)Roles.Provider);这就是我注释掉这一行的原因。var roleModel的作用域在if语句中,但我们在if之外重新使用它。如何使它在if之外可访问。@Wasfa-很抱歉,我刚刚更新了代码,以便在if之外可访问它。另外,我认为您不需要ModelState.AddModelError(“所有角色列表”);主要是因为您不希望在返回所有角色时模型处于错误状态。谢谢Raj。我还想访问控制器http post方法中dropdownlist的选定值。请您指导我如何操作??
  public class RoleViewModel
  {
     public RoleViewModel()
     {}

     public string SelectedRole { get; set; }

     public IEnumerable<SelectListItem> RolesList { get; set; }
  }
 @using (Html.BeginForm("DisplayAllRoles", "Home"))
 {
     @Html.DropDownListFor(m => m.RolesList,
        new SelectList(
        Model.RolesList,
        "Text",
        "Value", Model.SelectedRole))


     <p><input type="submit" value="Save" /></p>
 }
    [HttpPost]
    public ActionResult DisplayAllRoles(FormCollection form) {
        var selectedValue = form["RolesList"];
        return View();
    }