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