繁体   English   中英

使用通用类型&#39;RoleManager <TRole, TKey> &#39;需要2个类型参数

[英]Using the generic type 'RoleManager<TRole, TKey>' requires 2 type arguments

我遇到3个错误

使用泛型类型'RoleManager'TRole,TKey需要2个类型参数

在asp.net mvc 5 Web应用程序中我的Create方法的以下行中

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

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

如何解决这个问题

这里是整个创建方法:

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

这基本上意味着使用RoleManager时必须指定两种类型

RoleManager<string,string> //example

这类似于您使用Dictionary

Dictionary dict = new Dictionary(); //invalid
Dictionary<string,string> dict = new Dictionary<string,string>(); //valid

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM