好的,這是我的問題。我試圖用我的角色填充@Html.DropDownListFor()
減去Admin
角色。這工作得很好,但它顯示的所有角色:使用MVC中的自定義方法填充@ Html.DropDownListFor()
@Html.DropDownListFor(m => m.RoleName, new SelectList(Roles.GetAllRoles()))
然而,這顯示了所有的角色,包括Admin
卷。
所以我創建了另一個類UserHelper.cs
這個方法是basicly同樣的事情Roles.GetAllRoles()
:
public string[] GetUserRoles()
{
string[] userroles = null;
using (MainVeinDataDataContext conn = new MainVeinDataDataContext())
{
userroles = (from r in conn.Roles
where r.Rolename != "Admin"
select r.Rolename).ToArray();
}
return userroles;
}
然而,作爲很新的MVC,我不知道如何將這種方法暴露於DropDownList的在視圖中。所以這不工作,無論我嘗試:
@Html.DropDownListFor(m => m.RoleName, new SelectList(GetUserRoles()))
我不知道我失蹤了什麼,它讓我發瘋。希望有人知道我錯過了什麼。
你有一個視圖模型對這一觀點? – Dirk