【问题标题】:ASP.NET MVC Displaying a user's roles as comma separated stringASP.NET MVC 将用户的角色显示为逗号分隔的字符串
【发布时间】:2017-05-29 20:08:08
【问题描述】:

我想在 ASP.NET MVC5 中显示我的用户列表,并在列中显示用户拥有的每个角色(逗号分隔)。

所以我有一个简单的 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 方法。

感谢您的帮助!

【问题讨论】:

  • 未经测试,但尝试类似Roles = String.Join(", ", u.Roles.Select(r => r.Name))
  • @SteveGreene 'IdentityUserRole' 类没有'Name' 参数

标签: asp.net-mvc entity-framework linq


【解决方案1】:

您将需要一个 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))
        })


    );
}

【讨论】:

  • System.NotSupportedException: LINQ to Entities does not recognize the method 'System.String Join(System.String, System.Collections.Generic.IEnumerable'1[System.String])' method, and this method cannot be translated into a store expression.
  • 还是不行:System.NotSupportedException: 'LINQ to Entities does not recognize the method 'Microsoft.AspNet.Identity.EntityFramework.IdentityRole FindById[IdentityRole,String](Microsoft.AspNet.Identity.RoleManager'2[Microsoft.AspNet.Identity.EntityFramework.IdentityRole,System.String], System.String)' method, and this method cannot be translated into a store expression.'
【解决方案2】:

我找到了一个可行的解决方案。

// 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))
            }));
        }

【讨论】:

    【解决方案3】:

    试试

    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)
    

    【讨论】:

    • 我得到 System.NotSupportedException: 'LINQ to Entities 无法识别方法 'System.String Aggregate[String](System.Linq.IQueryable1[System.String], System.Linq.Expressions.Expression1[System.Func`3[System. String,System.String,System.String]])' 方法,并且此方法无法转换为存储表达式。当我尝试使用 foreach 循环显示行时,在视图中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-24
    • 1970-01-01
    • 1970-01-01
    • 2019-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多