2017-11-22 15 views
0

내 _EditUser보기에 사용자 프로필을 표시 한 다음 사용자 역할 (예 : 사용자 역할)을 편집하고 싶습니다. 이름 및 전자 메일.Asp.Net Mvc에서 사용자 역할과 같은 사용자 프로필을 편집하는 방법은 무엇입니까?

하지만 사용자 역할에 관해서는 충돌합니다.

model.ApplicationRoleId = RoleManager.Roles.Single(r => r.Name == UserManager.GetRoles(id).Single()).Id; 

이 내 내 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; } 
    } 

입니다 그리고 이것은 내보기에서 내 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(); 



    if (!String.IsNullOrEmpty(id)) 
    { 
     ApplicationUser user = await userManager.FindByIdAsync(id); 
     if (user != null) 
     { 
      model.Name = user.Name; 
      model.Email = user.Email; 
      model.ApplicationRoleId = RoleManager.Roles.Single(r => r.Name == UserManager.GetRoles(id).Single()).Id; // Here crashing .. I don't know why.. Server 500 error 
      ViewBag.RoleId = new SelectList(RoleManager.Roles, "Id", "Name", model.ApplicationRoleId); 
     } 
    } 
    return PartialView("_EditUser", model); 
} 

이다 : 나는 그것은 여기 충돌 500 (내부 서버 오류) 를 얻을 "_EditUser.cshtml"페이지 사용자 역할의 내 드롭 다운 목록은 다음과 같습니다.

<div class="form-group"> 
       @Html.Label("Role", htmlAttributes: new { @class = "control-label col-md-6" }) 
       <div class="col-md-12" > 
        @Html.DropDownList("RoleId", null, htmlAttributes: new { @class = "form-control" }) 
        @Html.ValidationMessageFor(model => model.ApplicationRoles, "", new { @class = "text-danger" }) 
       </div> 
      </div> 

/는

+0

은'Single' 방법 너희들 감사 @Shyju @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 .... 

}에 쓴이 같은 짓 – Shyju

+0

@Shyju 제게 해결책을 물어볼 수 있겠습니까? b/c 두 번째 질문입니다. 나는 충분히 뒤죽박죽이지만 그것을 해결할 수 없었다. –

+0

@ (내부 서버 오류) – DaniDev

답변

0

사전

에 감사드립니다 메서드를 호출하는 컬렉션이 비어 있거나 조건과 일치하는 둘 이상의 요소를 반환하면 예외가 throw됩니다.