2017-12-12 1 views
0

私はMVCを初めて使用しています。選択したユーザーのデータを表示するためのViewModelを持つ詳細ビューがあります。私はユーザーが持っているロールをリストしたいと思いますし、ロールの説明とロールのIDも必要です。選択したユーザーの説明でロールを取得

私はこれで、選択しApplicationUserのための役割を取得することができる午前:私は各役割の説明を取得し、私の詳細ビューに表示する方法がわからない

model.UserRoles = await _userManager.GetRolesAsync(model.AppUser); 

ありがとうございます。

答えて

1

私はそれを理解しました。これが誰かに役立つことを願っています。私はこれが最高か最も効率的な方法かどうかはわかりませんが、それは機能しています。私は、次のとユーザーロールを得た:

//Get the User Roles 
 
IList<String> userRoles = await _userManager.GetRolesAsync(model.AppUser);

私は役割の説明を取得するためにこれを使用します。ここでは

List<String> roleDescription = new List<String>(); 
 

 
if (userRoles.Count > 0) 
 
{ 
 
    foreach (string r in userRoles) 
 
    { 
 
     //Get the Description for the assigned Role 
 
     var desc = _roleManager.Roles.Where(n => n.Name == r).Select(d => d.Description).Single(); 
 
     roleDescription.Add(desc.ToString()); 
 
    } 
 

 
    //Using Tuple to combine text in [Role] | [Description] format in View 
 
    var roleWithDesc = new List<Tuple<string, string>>(); 
 

 
    for (int i = 0; i < userRoles.Count; i++) 
 
    {  
 
     roleWithDesc.Add(Tuple.Create(userRoles[i].ToString(), roleDescription[i].ToString())); 
 
    } 
 

 
    model.UserRoles = roleWithDesc; 
 
}

は私のコードですViewModel:

public ApplicationUser AppUser { get; set; } 
 

 
public Employee Employee { get; set; } 
 

 
[Display(Name = "User Roles")] 
 
public List<Tuple<string, string>> UserRoles { get; set; } 
 

 
[Display(Name = "Available Roles")] 
 
public List<Tuple<string, string>> AllRoles { get; set; }

関連する問題