1
我想将数据库中的所有角色显示到下拉列表中。我以这种方式覆盖了角色提供者的GetAllUser方法。将数据库中的所有角色显示到下拉列表中
public string[] GetAllRoles()
{
string[] roles = Session.Query<RoleManager>()
.Select(r => r.roleName)
.ToArray();
return roles;
}
我在Controller中调用了这个方法。
[HttpGet]
public ActionResult DisplayAllRoles()
{
string[] allRoles = ((CustomRoleProvider)Roles.Provider).GetAllRoles();
if (allRoles != null)
{
//bind dropDownlist list in view with all roles ???
ModelState.AddModelError("", "List of All roles");
}
else ModelState.AddModelError("","No role exists.");
return View();
}
查看:
@Html.DropDownListFor(m => m.AllRoles,new SelectList (Model.AllRoles))
现在我的问题是,如何从roles.Can的那个字符串数组填充一个下拉列表请你写我的情况的示例代码。
我不想硬编码角色值。在我的情况下,字符串数组allroles包含我想填充下拉列表的角色。 – Wasfa
当然。在你的例子中,硬编码值将被真正的实现字符串[] allRoles =((CustomRoleProvider)Roles.Provider)替换.GetAllRoles();这就是为什么我评论这一行。 – Spock
var scopeModel的范围在if语句内,但我们正在外面回应if.How可以在外部访问if。 – Wasfa