troygoode / MembershipStarterKit

The starter kit provides the Asp.Net MVC controllers, models, and views needed to administer users & roles.
http://github.com/TroyGoode/MembershipStarterKit
MIT License
197 stars 66 forks source link

Display of users in a specific role - null reference after user delete #20

Closed Thieum closed 12 years ago

Thieum commented 13 years ago

When you delete a user through this method :

  public RedirectToRouteResult DeleteUser(Guid id)
  {
     _userService.Delete(_userService.Get(id));
     return RedirectToAction("Index");
  }

Only the membership part of the user is deleted. The user itself remains. It's not so much of an issue in the rest of the application except for the view displaying all the users for a specific role :

  public ViewResult Role(string id)
  {
     return View(new RoleViewModel
                 {
                    Role = id,
                    Users = _rolesService.FindUserNamesByRole(id).Select(username => _userService.Get(username))
                 });
  }

It will pop a null reference as the user will still exist in the list return by _rolesService.FindUserNamesByRole(id) but not by _userService.Get(username) which leads to this version where it is tested :

  public ViewResult Role(string id)
  {
     return View(new RoleViewModel
                 {
                    Role = id,
                    Users = _rolesService.FindUserNamesByRole(id).Where(username => (_userService.Get(username) != null)).Select(username => _userService.Get(username))
                 });
  }
troygoode commented 12 years ago

nice catch. fixed