【问题标题】:ASP.NET Identity 2.0 Get RoleNames in a dropdown listASP.NET Identity 2.0 在下拉列表中获取 RoleNames
【发布时间】:2014-08-12 14:34:55
【问题描述】:

我想要下拉列表中的角色名称,但我的代码将其放入:System.Data.Entity.DynamicProxies.IdentityRole_9242DF3B1E41249C78E71E10BE06DC7180880D3BD461D49C4D7FA49EA1C455CA

        var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>());
        var roles = roleManager.Roles.ToList();

        DropDownList1.DataSource = roles;
        DropDownList1.DataBind();

我觉得这是我看不到的非常简单的错误。

感谢 Jeremy Cook 提供的正确答案。

新代码

            var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>());
        var roles = roleManager.Roles.ToList();
        DropDownList1.DataTextField = "Name";
        DropDownList1.DataValueField = "Id";
        DropDownList1.DataSource = roles;
        DropDownList1.DataBind();

【问题讨论】:

    标签: c# asp.net entity-framework asp.net-identity


    【解决方案1】:

    不幸的是IdentityRole 没有覆盖ToString(),所以你看到的是类名。 See the source here.

    但是,将DataTextField 属性设置为“名称”应该可以避免您的麻烦。 (您可能还想将DataValueField 属性设置为“Id”。)

    【讨论】:

    • 这似乎与 ToString() 的覆盖无关。如果数据源包含多个数据字段,则必须指定将哪一个绑定为下拉列表的文本字段,并可选择将哪一个绑定为值字段,否则控件将不知道要绑定哪个字段到。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-12
    • 1970-01-01
    相关资源
    最近更新 更多