0

I want to show user profile to my _EditUser view and then to edit user profile such as user role .. Name and Email.

But when it comes to user role it's crashing. I get 500 (Internal Server Error) It is crashing here:

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

This is my is my 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; }
    }

And this is my EditUser Action

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

In my View page "_EditUser.cshtml" My Dropdownlist of user role looks like this

<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>

/ Thank you in advance

Helen Tekie
  • 515
  • 1
  • 6
  • 23
  • The `Single` method will throw an exception when the collection (on which you are calling the method) is empty or returns more than one element matching the condition. – Shyju Nov 22 '17 at 18:05
  • @Shyju Can you please give me farther answer with solution, b/c it's second time I'am asking. I have enough straggled but couldn't solve it. – Helen Tekie Nov 22 '17 at 18:14
  • Please post the error beyond 500 (Internal Server Error) – DaniDev Nov 22 '17 at 18:19
  • @DaniDev GET ....Account/EditUser?id=683c1264-075f-4d51-9346-b9349fb23bea 500 (Internal Server Error) jquery-3.1.1.js:9536 – Helen Tekie Nov 22 '17 at 18:25
  • 1. If you know that your app is "crashing" at the above line then you can point a break point there. and you should be able to get detailed message about what is causing the error. 2. use SingleOrDefault() instead of Single() ? – DaniDev Nov 22 '17 at 18:36

1 Answers1

0

I did like this as @Shyju wrote in my old question

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 ....

}

Thank you guys and @Shyju

Helen Tekie
  • 515
  • 1
  • 6
  • 23