我在填补用户角色时遇到问题,我找不到解决方案。我有用户角色列表,但我不知道如何将其发送到模型。我的代码:
ApplicationUser
namespace WebApp.Models
{
public class ApplicationUser : IdentityUser
{
public ApplicationUser() : base() { }
public string FirstName { get; set; }
public string LastName { get; set; }
public IEnumerable<ApplicationRole> UserRoles { get; set; }
}
}
ApplicationRole
namespace WebApp.Models
{
public class ApplicationRole : IdentityRole
{
public ApplicationRole() : base() { }
public ApplicationRole(string roleName) : base(roleName) { }
public ApplicationRole(string roleName, string description, DateTime creationDate) : base(roleName)
{
this.Description = description;
this.CreationDate = creationDate;
}
public string Description { get; set; }
public DateTime CreationDate { get; set; }
}
}
AdminController
public async Task<IActionResult> UserList(ApplicationUser ApplicationUser)
{
var users = await _userManager.Users.ToListAsync();
foreach (var v in users)
{
var userId = await _userManager.GetUserIdAsync(v);
var user = _context.Users.Where(u => u.Id == userId).FirstOrDefault();
user.UserRoles = _userManager.GetRolesAsync(v);
_context.SaveChanges();
}
var usersList = _context.Users.ToList();
return await Task.Run(() => View(usersList));
}
user.UserRoles = _userManager.GetRolesAsync(v);
导致错误
严重性代码描述项目文件行抑制状态 错误CS0266无法将类型'System.Threading.Tasks.Task>'隐式转换为'System.Collections.Generic.IEnumerable'。存在显式转换(您是否缺少演员表?)
// EDIT错误是由于缺少asynch引起的,但现在的错误是
严重性代码描述项目文件行抑制状态 错误CS0266无法将类型'System.Collections.Generic.IList'隐式转换为 'System.Collections.Generic.IEnumerable'。 存在明确的转换(您是否缺少演员表?)WebApp
如何转换数据以填充UserRoles?