创建下拉列表时不支持每种类型的多个对象集

Multiple object sets per type are not supported when i create a dropdown list

当我使用此代码

在标识表 AspNetRole 中创建下拉列表时

IdentityConfig.cs

public class ApplicationRoleManager : RoleManager<IdentityRole>
{
    public ApplicationRoleManager(IRoleStore<IdentityRole, string> roleStore)
        : base(roleStore)
    {
    }

    public static ApplicationRoleManager Create(IdentityFactoryOptions<ApplicationRoleManager> options, IOwinContext context)
    {
        return new ApplicationRoleManager(new RoleStore<IdentityRole>(context.Get<ApplicationDbContext>()));
    }
}

Startup.Auth.cs:

app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);

.

.private ApplicationRoleManager _roleManager;
public ApplicationRoleManager RoleManager
{
    get
    {
        return _roleManager ?? HttpContext.GetOwinContext().Get<ApplicationRoleManager>();
    }
    private set
    {
        _roleManager = value;
    }
}

控制器:

        [AllowAnonymous]
    public ActionResult Register()
    {
        ViewBag.name = new SelectList(db.Roles, "RoleID", "RoleName");

        return View();
    }

查看:

<div class="form-group">
    <label>نقش</label>
    <div class="col-md-10">
      @Html.DropDownList("name", null, htmlAttributes: new { @class = "form-control" })
    </div>
</div>

它告诉我这个错误:

我该如何解决?

/******************************************** ****************************************************** **/

控制器

[AllowAnonymous]
public ActionResult Register() {
    ViewBag.Roles = new SelectList(db.Roles.ToList(), "Id", "Name");

    return View();
}

[AllowAnonymous]
public ActionResult Register() {
    var roles = db.Roles.Select(r => new { RoleID = r.Id, RoleName = r.Name}).ToList();
    ViewBag.Roles = new SelectList(roles, "RoleID", "RoleName");

    return View();
}

查看:

<div class="form-group">
    <label>نقش</label>
    <div class="col-md-10">
      @Html.DropDownList("SelectedRole", (IEnumerable<SelectListItem>)ViewBag.Roles, htmlAttributes: new { @class = "form-control" })
    </div>
</div>

尝试使用此代码,更改

ViewBag.name = new SelectList(db.Roles.ToList(), "RoleID", "RoleName");

和您的下拉列表

@Html.DropDownList("name", (IEnumerable<SelectListItem>)ViewBag.name, htmlAttributes: new { @class = "form-control" })