0

I found a question that is very similar to mine.

MVC5 Account Controller null reference exception

I was going to just comment on that question and ask this, but alas I don't have enough points to comment.

This is my RoleController

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult RoleAddToUser(string UserName, string RoleName)
    {
        ApplicationUser user = context.Users.Where(u => u.UserName.Equals(UserName, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();
         // var account = new AccountController();
        // if (user!=null) UserManager.AddToRole(user.Id, RoleName);
        if (user != null) UserManager.AddToRole(user.Id, RoleName);


        ViewBag.ResultMessage = "Role created successfully !";

        // prepopulat roles for the view dropdown
        var list = context.Roles.OrderBy(r => r.Name).ToList().Select(rr => new SelectListItem { Value = rr.Name.ToString(), Text = rr.Name }).ToList();
        ViewBag.Roles = list;

        return View("ManageUserRoles");
    }

This is my ManageUserRoles.cshtml view page

    @{
       ViewBag.Title = "ManageUserRoles";
     }

    <h2>Manage User Roles</h2>
    @Html.ActionLink("Create New Role", "Create") | @Html.ActionLink("Manage User Role", "ManageUserRoles")
    <hr />
       <h2>Role Add to User</h2>

      @using (Html.BeginForm("RoleAddToUser", "Role"))
     {
       @Html.AntiForgeryToken()
       @Html.ValidationSummary(true)

      <p>
       Username : @Html.TextBox("UserName")
       Role Name: @Html.DropDownList("RoleName", (IEnumerable<SelectListItem>)ViewBag.Roles, "Select ...")

     </p>

<input type="submit" value="Save" />
 }
   <hr />
   <h3>Get Roles for a User</h3>
   @using (Html.BeginForm("GetRoles", "Role"))
   {
       @Html.AntiForgeryToken()
   <p>
    Username : @Html.TextBox("UserName")
    <input type="submit" value="Get Roles for this User" />
</p>
    }

   @if (ViewBag.RolesForThisUser != null)
   {
       <div style="background-color:yellow;">
       <h3>Roles for this user </h3>
       <ol>
           @foreach (string s in ViewBag.RolesForThisUser)
        {
            <li>@s</li>
        }
    </ol>
</div>
    }

    <hr />
    <h3>Delete A User from a Role</h3>

    @using (Html.BeginForm("DeleteRoleForUser", "Role"))
   {
      @Html.AntiForgeryToken()
      @Html.ValidationSummary(true)

  <p>
    Username : @Html.TextBox("UserName")
    Role Name: @Html.DropDownList("RoleName", (IEnumerable<SelectListItem>)ViewBag.Roles, "Select ...")

</p>

<input type="submit" value="Delete this user from Role" />
}

My AccountController and Startup.Auth.cs are the same as the question in the post I linked above. I tried the answer that was accepted on that question, and when I tried to get rid of the "account" before UserManager.AddToRole it underlined UserManager with the error "Using the generic type 'UserManager' requires 2 type arguments".

When I add the account before UserManager.AddToRole (the lines of code I have commented out) I get NullReferenceException error "Object Reference not set to an instance of an object" in the AccountController for the line:

return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();

I am completely at a loss!

Community
  • 1
  • 1
Jules Mena
  • 67
  • 8
  • 1
    where have you initialized UserManager? – Mukesh Modhvadiya Apr 20 '17 at 20:06
  • Are you sure your `AccountController` is the same with the one in the referenced answer? Because the default constructor has nothing in its body (it is declared like this `AccountController(){}`) and the `NullReferenceException` would not be thrown unless you are calling `account.UserManager.AddToRole(user.Id, RoleName);` – granit Apr 20 '17 at 20:26

0 Answers0