将 Dictionary(Integer, String) 绑定到 DropDownList 的 DataTextField 属性

发布于 2024-09-29 09:42:05 字数 587 浏览 3 评论 0原文

我有一个 State 类定义如下:

Public Class State
    Public Property StateId As Integer
    Public Property Name As Dictionary(Of Integer, String)
End Class

Name(x) 包含不同语言的状态名称。

我从方法 StateManager.GetAllStates() 获取状态集合 我想将此集合绑定到 DropDownList。问题是 我找不到如何将 DataTextField 属性设置为 stateList.Name(1) 这是我所在州的英文名称。

Dim stateList As StateCollection = StateManager.GetAllStates() 

Me.DataSource = stateList
Me.DataValueField = "StateId"
Me.DataTextField = "Name(1).Value" <-- Problem here
Me.DataBind()

有人有主意吗?

谢谢

I have a State class defined like this:

Public Class State
    Public Property StateId As Integer
    Public Property Name As Dictionary(Of Integer, String)
End Class

Name(x) contains the state name in different languages.

I get a collection of State from the method StateManager.GetAllStates()
and I want to bind this collection to a DropDownList. The problem is that
I can't find how to set the DataTextField property to let's say stateList.Name(1)
which is the english name of my state.

Dim stateList As StateCollection = StateManager.GetAllStates() 

Me.DataSource = stateList
Me.DataValueField = "StateId"
Me.DataTextField = "Name(1).Value" <-- Problem here
Me.DataBind()

Anyone have an idea?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟沫凡尘 2024-10-06 09:42:05

使用数据源时,不能使用复杂的表达式来指定值。

创建一个源,您可以在其中提取所需的值:

Me.DataSource = stateList.Select( _
  Function(s as State) New With { .Id = s.StateId, .Name = s.Name(1) } _
);
Me.DataValueField = "Id"
Me.DataTextField = "Name"

When you use a data source, you can't use complex expressions to specify values.

Create a source where you extact the values that you need:

Me.DataSource = stateList.Select( _
  Function(s as State) New With { .Id = s.StateId, .Name = s.Name(1) } _
);
Me.DataValueField = "Id"
Me.DataTextField = "Name"
此刻的回忆 2024-10-06 09:42:05

您是否尝试过而不是“Name(1).Value”:

Me.DataTextField = name(1).ToString

Rather than "Name(1).Value", have you tried:

Me.DataTextField = name(1).ToString
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文