2017-05-26 25 views
0

我想在ASP.NET MVC5中顯示我的用戶列表,並在列中顯示用戶擁有的每個角色(以逗號分隔)。ASP.NET MVC以逗號分隔的字符串顯示用戶的角色

所以我有一個簡單的UserViewModel:

public class UserViewModel 
    { 
     public string UserName { get; set; } 
     public string Email { get; set; } 
     public string AdatlapNev { get; set; } 
     public string Roles { get; set; } 

    } 

在我看來,該模型是:@model IEnumerable<UserViewModel>

問題是與角色的顯示。我試圖這樣做:

public ActionResult UserList() 
     { 
      return View(db.Users.Select(u => new UserViewModel 
      { 
       AdatlapNev = u.Adatlap == null ? "NULL" : u.Adatlap.Nev, 
       Email = u.Email, 
       UserName = u.UserName, 
       Roles = u.Roles.Select(r => db.Roles.Find(r.RoleId).Name).Aggregate((s1, s2) => s1 + ", " + s2) 
     } 
      )); 
     } 

但它不起作用。我得到System.ArgumentException其中說,我不能在這裏使用Find方法。

感謝您的幫助!

+0

未經測試,但嘗試類似'Roles = String.Join(「,」,u.Roles.Select(r => r.Name))''。 –

+0

@SteveGreene'IdentityUserRole'類沒有'Name'參數 – szkup

回答

0

我發現它有效的解決方案。

// GET: Admin/UserList 
     public ActionResult UserList() 
     { 
      var rm = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(db)); 

      var q = db.Users.Select(u => new 
      { 
       AdatlapNev = u.Adatlap == null ? "NULL" : u.Adatlap.Nev, 
       Email = u.Email, 
       UserName = u.UserName, 
       Roles = u.Roles.Select(r => r.RoleId) 
      }); 

      var roles = db.Roles.Select(r => new { Id = r.Id, Name = r.Name }).ToList(); 

      return View(q.AsEnumerable().Select(x => new UserViewModel 
      { 
       AdatlapNev = x.AdatlapNev, 
       Email = x.Email, 
       UserName = x.UserName, 
       Roles = String.Join(", ", x.Roles.Select(j => roles.Where(r => r.Id == j).Single().Name)) 
      })); 
     } 
0

嘗試

db.Roles.Where(x => u.Roles.Any(y => x == y.RoleId)).Select(x => x.Name) 

,而不是

u.Roles.Select(r => db.Roles.Find(r.RoleId).Name) 
+0

我得到System.NotSupportedException:'LINQ to Entities does not recognized the method'System.String Aggregate [String](System.Linq.IQueryable'1 [ System.String],System.Linq.Expressions.Expression'1 [System.Func'3 [System.String,System.String,System.String]])方法,並且此方法不能轉換爲存儲表達式。'在視圖中,當我嘗試用foreach循環顯示行時。 – szkup

2

您將需要一個RoleManager得到名稱:

public ActionResult UserList() 
{ 

    var rm = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(db)); 

    return View(
     db.Users.Select(u => new UserViewModel 
     { 
      AdatlapNev = u.Adatlap == null ? "NULL" : u.Adatlap.Nev, 
      Email = u.Email, 
      UserName = u.UserName, 
      Roles = String.Join(", ", u.Roles.Select(r => rm.FindById(r.RoleId).Name)) 
     }) 


    ); 
} 
+0

System.NotSupportedException:LINQ to Entities無法識別方法'System.String加入(System.String,System.Collections.Generic.IEnumerable'1 [System.String])''方法,並且此方法無法轉換爲存儲表達式。' – szkup

+0

https://stackoverflow.com/questions/9647039/linq-cant-join-to-string –

+0

仍然無法正常工作:'System.NotSupportedException:'LINQ to Entities does not recognized the method'Microsoft。 AspNet.Identity.EntityFramework.IdentityRole FindById [IdentityRole,String](Microsoft.AspNet.Identity.RoleManager'2 [Microsoft.AspNet.Identity.EntityFramework.IdentityRole,System.String],System.String)'方法,並且此方法不能被翻譯成商店表達 – szkup