0

I'm getting 3 errors

Using the generics type 'RoleManager' TRole,TKey requires 2 type arguments

at following line of my Create method in asp.net mvc 5 web application

ViewBag.RoleId = new SelectList(await RoleManager.Roles.ToListAsync("Name", "Name"));  

ViewBag.RoleId = new SelectList(RoleManager.Roles, "Name", "Name");

how to fix this

here the whole create method:

    // POST: /Account/Register
    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Register(RegisterViewModel model, HttpPostedFileBase upload, params string[] selectedRoles)
    {

    try
    {
        if (ModelState.IsValid)
        {
            var user = new ApplicationUser { UserName = model.UserName, Email = model.Email };                      


            var result = await UserManager.CreateAsync(user, model.Password);

            if (result.Succeeded)
            {
                //Add User to the selected Roles 
                if (selectedRoles != null)
                {
                    var addroles = await UserManager.AddToRolesAsync(user.Id, selectedRoles);
                    if (!addroles.Succeeded)
                    {
                        ModelState.AddModelError("", result.Errors.First());
                        ViewBag.RoleId = new SelectList(await RoleManager.Roles.ToListAsync("Name", "Name"));
                        return View();
                    }
                }

            }

            else
            {
                ModelState.AddModelError("", result.Errors.First());
                ViewBag.RoleId = new SelectList(RoleManager.Roles, "Name", "Name");
                return View();
            }

            return RedirectToAction("Index");
            // AddErrors(result);
        }

    }

    // If we got this far, something failed, redisplay form
    catch (RetryLimitExceededException /* dex */)
    {
        //Log the error (uncomment dex variable name and add a line here to write a log.
        ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator.");
    }

    ViewBag.RoleId = new SelectList(RoleManager.Roles, "Name", "Name");

    return View(model);
}
kez
  • 2,273
  • 9
  • 64
  • 123

1 Answers1

1

It basically means that you have to specify two types when using RoleManager

RoleManager<string,string> //example

This is similar to how you use Dictionary

Dictionary dict = new Dictionary(); //invalid
Dictionary<string,string> dict = new Dictionary<string,string>(); //valid
Ian
  • 30,182
  • 19
  • 69
  • 107
  • @kez find "it" and "edit"? Sorry, I don't get you... basically you need to specify the two types for your `RoleManager`. You use `RoleManager` in three places in your code, therefore you got three errors. Change all your `RoleManager` to `RoleManager` – Ian Mar 04 '16 at 07:08
  • where should I change `RoleManger` ? in this controller method or within the rolemanger class ? – kez Mar 04 '16 at 10:03
  • this is the [RoleManager Class](https://bitbucket.org/snippets/Common_Admin/xre6A) – kez Mar 04 '16 at 10:08
  • @kez Yes, I have seen the code. In the `Controller`. you only need to use your `RoleManager` class with type. Something like `RoleManager` or ``RoleManager` – Ian Mar 04 '16 at 12:40
  • pardon me , is that something like this `ViewBag.RoleId = new SelectList(await RoleManager.Roles.ToListAsync("Name", "Name"));` ? but then I'm having error also – kez Mar 04 '16 at 12:45
  • No, I mean, on the instance of `RoleManager`, not on the `class`. What is the type of your `RoleManager.Roles` instance? is it `RoleManager`? If it is, then you should do it in the `RoleManager.Roles` – Ian Mar 04 '16 at 12:51
  • Isthat Include in `IdentityConfig.cs` file ? [here the only place](https://bitbucket.org/snippets/Common_Admin/xre6A#file-identityconfig.cs) I can see RoleManager referenced – kez Mar 04 '16 at 13:02
  • In the `IdentifyConfig.cs` in only shows that you have `ApplicationRoleManager` derives from `RoleManager`, I don't think it is the problem. What I mean is the error that you get. In which line do you get the error? That is the candidate for the fix. – Ian Mar 04 '16 at 13:13
  • no no , I'm getting errors at Register Method , Actually , here I mentioned whole code [in this question](http://stackoverflow.com/questions/35765232/assign-multiple-roles-for-a-user-in-aspnet-identity) , can you let me know ,whats the place should I configure – kez Mar 04 '16 at 13:20
  • @kez in that case, please find where is the instance of type `RoleManager` (or its derivation) in the `Register` method. Your error is likely caused by it. I really suspect your `Roles` (or its methods) to be one of the prime candidate... – Ian Mar 04 '16 at 13:25
  • Actually I followed [this tutorial](http://johnatten.com/2014/06/22/asp-net-identity-2-0-customizing-users-and-roles/) , but I cannot see there any instance creation using `Rolemanger` Class – kez Mar 04 '16 at 13:32