2017-10-14 2 views
0

사용자 프로필을 편집하고 싶을 때 현재 사용자 역할 이름을 가져 오려면 Asp.Net MVC 코드에서 내 작업이 중단되는 이유는 무엇입니까? 이 코드는 내가 할Asp.Net Mvc 사용자 역할이있는 사용자 프로필 편집

model.ApplicationRoleId = roleManager.Roles.Single(r => r.Name == userManager.GetRolesAsync(user).Result.Single()).Id; 

오류 메시지입니다 충돌한다 : 500 (내부 서버 오류) 여기

가 내 EditUserview

public class EditUserViewModel 
{ 
    public string Id { get; set; } 
    public string Name { get; set; } 
    public string Email { get; set; } 
    public List<SelectListItem> ApplicationRoles { get; set; } 

    public string ApplicationRoleId { get; set; } 

    public List<SelectListItem> DepartmentNames { get; set; } 

    public int DeptId { get; set; } // I have DeptId too in my AspNetUser table 
} 

그리고 내 Edituser 행동

[HttpGet] 
public async Task<IActionResult> EditUser(string id) 
{ 
    EditUserViewModel model = new EditUserViewModel(); 
    model.ApplicationRoles = roleManager.Roles.Select(r => new SelectListItem 
    { 
     Text = r.Name, 
     Value = r.Id 
    }).ToList(); 

    model.DepartmentNames = context.Departments.Select(s => new SelectListItem 
    { 

     Text = s.DeptName, 
     Value = s.DeptId.ToString() 
    }).ToList(); 

    if (!String.IsNullOrEmpty(id)) 
    { 
     ApplicationUser user = await userManager.FindByIdAsync(id); 
     if (user != null) 
     { 
      model.Name = user.Name; 
      model.Email = user.Email; 
      model.DeptId = user.DepartmentId; 
      //model.ApplicationRoleId crashing 
      model.ApplicationRoleId = roleManager.Roles.Single(r => r.Name == userManager.GetRolesAsync(user).Result.Single()).Id; // Here crashing .. I don't know why.. Server 500 error 

      ViewBag.RoleId = new SelectList(RoleManager.Roles, "Id", "Name", model.ApplicationRoleId); 
      ViewBag.DeptId = new SelectList(db.Departments, "Deptd", "DeptName", model.DeptId); 


     } 
    } 
    return PartialView("_EditUser", model); 
} 

나는 이런 식으로 시도했지만이 충돌도 ..

string existingRole = UserManager.GetRolesAsync(user.Id).Result.Single();// This crashing 

여기에 넣어 :

string existingRoleId = RoleManager.Roles.Single(r => r.Name == existingRole).Id; 

을하지만, 문자열 existingRole 나를 위해 작동하지 않았다 ..

내가 어떤 도움을 greateful 것 ..

+0

'결과'를 사용하여 비동기 메서드를 호출하면 안됩니다. 'var r = await UserManager.GetRolesAsync (user.Id);와 같은 메소드를 기다린다. var existingRole = u.First(); 또한 콜렉션에 항상 하나의 항목 만 있다고 확신 할 때 Single 메소드를 사용하십시오. 그렇지 않으면'InvalidOperationException' 예외가 발생합니다. – Shyju

+0

@Shyju 답장을 보내 주셔서 감사합니다.하지만 var r = UserManager.GetRolesAsync (user.Id);를 (를) 기다리는 것은 무엇을 의미합니까? 이 결과를 어디에 둘까요? 당신은 model.ApplicationRoleId = wait UserManager.GetRolesAsync (user.Id);를 의미합니까? 그리고 나서 var existingRole = u.First()? 너 어디로 간다? 어딘가에 선언해야합니다 .. 내 모델을 어떻게 초기화해야합니까? 응용 프로그램의 역할은 무엇입니까? .다시 감사합니다. –

+0

@Shyju, 제발 도와 줄 수 있니 ..?. –

답변

0

을 나는이 좋아했다 @Shyju 님이 작성한대로

var role = await UserManager.GetRolesAsync(user.Id); 
         var existingRole = role.First(); 
    if (existingRole != null) 
     { 
    string existingRoleId = RoleManager.Roles.Single(r => r.Name == existingRole).Id; 
    model.ApplicationRoleId = existingRoleId; 
    ..... and so on .... 

    } 

고마워요. u @Shyju