2017-06-29 37 views
0

對C#和ASP.net沒有多少經驗。獲取不是「用戶」但註冊了我的系統的人列表

嘗試生成等待分配「用戶」狀態的人員列表,但我剛剛向系統中的每個人顯示,Authoirzed用戶和未經授權的人員。

我控制器的方法是:

//GET 
    public ActionResult UsersNoRoles() 
    { 
     var users = (from u in db.UserProfiles 
        select u); 


     IList<UserProfile> usersNoRoles = new List<UserProfile>(); 


     foreach (var item in users) 
     { 

      SelectList list = new SelectList(Roles.GetUsersInRole(item.UserName)); 

      if (list.Count() == 0) 
      { 
       usersNoRoles.Add(item); 

      } 
     } 


     return View(usersNoRoles); 
    } 

然後我送它到我的視圖中的表:

@if ((Model != null) && (TempData["Error"] == null)) 
{ 
<tr> 
    <th> 
     Username 
    </th> 
    </tr> 
     foreach (var item in Model) 
     { 
     <tr> 
      <td> 
       @Html.DisplayFor(modelItem => item.UserName) 
      </td> 
     </tr> 
} 

我已經指示不使用視圖模型也。

我想要一個未經授權的人員列表。

+4

什麼是你的問題? –

+0

如何獲得未在列表中授權的用戶 –

+0

用戶的哪個屬性指示該用戶是否已註冊。 –

回答

0

您的代碼坦言...破

foreach (var item in users) 
    { 
     // this is broken and doesnt add any functionality should simply go away, 
     // item.UserName shouldn't have anything to do with role of the user, 
     // you are creating this list for every single iteration trough the userlist 
     SelectList list = new SelectList(Roles.GetUsersInRole(item.UserName)); 

     // here you are adding the user to your list only if its empty, by your result it seems like it's always empty 
     // that means that the previous statement in your code doesn't really 
     // filter anything 
     if (list.Count() == 0) 
     { 
      usersNoRoles.Add(item); 

     } 
    } 

讓我們做這樣的:

var usersNoRoles = new List<UserProfile>(); 

foreach(var user in users) 
{ 
    if(user.UserRole != "User") 
    usersNoRoles.Add(user); 
} 
+0

我使用開箱即用的代碼,因此我無法使用user.UserRole。 –

+0

所以你說你想根據他們的角色來過濾用戶,但你不能看他們有什麼角色?這是一個艱難的... – pijemcolu

+0

是的,告訴我關於它。過去幾天一直在破壞頭部。 –