MVC5 VB.Net 将 Dictionary(Of String,String) 转换为 SelectList 类型
MVC5 VB.Net Convert Dictionary(Of String,String) to SelectList type
我有一个简单的 VB 字典:
Dim MyOptions As New Dictionary(Of String, String) From {{"one", "ONE"}, {"two", "TWO"}, {"three", "THREE"}}
我正在尝试将其转换为 SelectList 类型,以便在 Html.DropDownList 帮助器中使用,该帮助器为 select 列表生成 HTML。
我试过这个:
Dim list As SelectList = New SelectList(MyOptions, "Key", "Value", "one") ' errors on this line
html.DropDownList("SomeList", list, "Simple List")
导致此错误:
DataBinding: 'System.String' does not contain a property with the name
'Key'.
我已经试过了:
Dim list As SelectList = New SelectList(MyOptions.[Select](Function(x) New With {Key .Value = x.Key, Key .Text = x.Value}), "Value", "Text") ' errors on this line
html.DropDownList("SomeList", list, "Simple List")
导致此错误:
Public member 'Key' on type 'String' not found.
编辑
我明显简化了上面的例子,有点简单。
在我实际代码的某个地方,字典(字符串,字符串)正在转换为 KeyValuePair(字符串,对象)。
实际代码:
在class中我定义了这个public属性:
Public InputFilters As List(Of Dictionary(Of String, Object)) = Nothing ' stores the details of all the added filter types to be rendered
在同一个 class 中,我有这个方法:
Private Sub _Add(ByVal type As String, ByVal labelText As String, ByVal id As String, ByVal defaultValue As Object, ByVal additionalInputClass As String, ByVal placeholder As String, ByVal arrValues As String(), ByVal dictValues As Dictionary(Of String, String), ByVal checked As Boolean)
Dim normalizedValues As New Dictionary(Of String, String)
If dictValues.Count > 0 Then
For Each key As String In dictValues.Keys
normalizedValues.Add(key, dictValues(key))
Next
End If
If arrValues.Count > 0 Then
For Each val As String In arrValues
normalizedValues.Add(val, val)
Next
End If
Dim d = New Dictionary(Of String, Object) From {
{"type", type},
{"labelText", labelText},
{"id", id},
{"defaultValue", defaultValue},
{"additionalInputClass", additionalInputClass},
{"checked", checked}, {"placeholder", placeholder},
{"commonInputClass", CommonInputClass},
{"idPrefix", IdPrefix}}
d.Add("Values", normalizedValues) ' breakpoint and watch here shows d("Values") as type Dictionary(Of String, String)
InputFilters.Add(d) ' breakpoint and watch here shows InputFilters(0)("Values") as type KeyValuePair(Of String, Object)
End Sub
注意最后两行的注释,因为这是从字典到 KeyValuePair 的转换发生的地方。
我在剃须刀视图页面中这样做:
@For Each Filter As Dictionary(Of String, Object) In Model.InputFilters
@Html.BuildFilterInput(Filter)
Next
BuildFilterInput 的 HTML 辅助代码如下所示:
<Extension()>
Public Function BuildFilterInput(ByVal html As HtmlHelper, ByVal Filter As Dictionary(Of String, Object)) As IHtmlString
Dim id As String = (Filter("idPrefix") & Filter("id")).ToString()
Dim Result As String = ""
Result &= "<div class=""col-md-6 col-lg-4 report-filter-item-wrapper"">" & vbCrLf
Result &= " <div class=""form-group"">" & vbCrLf
Result &= " " & html.Label(id, Filter("labelText").ToString).ToString() & vbCrLf
Result &= " "
Select Case Filter("type")
Case "text"
Result &= html.TextBox(id, Filter("defaultValue").ToString(), New With {Key .name = id, Key .[class] = "form-control" & If(Filter("additionalInputClass").ToString = "", "", " " & Filter("additionalInputClass").ToString()) & If(Filter("commonInputClass").ToString() = "", "", " " & Filter("commonInputClass").ToString()), Key .placeholder = If(Filter("placeholder").ToString() = "", "", Filter("placeholder").ToString())}).ToString() & vbCrLf
Case "select"
Dim list As SelectList = New SelectList(Filter.Values, "Key", "Value", "ME") ' this is where it blows up because Filter.Values is not a dictionary !!!!!!
Result &= html.DropDownList(id, list, "What is this").ToString() & vbCrLf
End Select
Result &= " </div>" & vbCrLf
Result &= "</div>" & vbCrLf
Return html.Raw(Result)
End Function
卫生部!这是语法错误
我正在使用 Filter.Values 试图访问 "Values" 键,我应该使用 Filter("Values").
我有一个简单的 VB 字典:
Dim MyOptions As New Dictionary(Of String, String) From {{"one", "ONE"}, {"two", "TWO"}, {"three", "THREE"}}
我正在尝试将其转换为 SelectList 类型,以便在 Html.DropDownList 帮助器中使用,该帮助器为 select 列表生成 HTML。
我试过这个:
Dim list As SelectList = New SelectList(MyOptions, "Key", "Value", "one") ' errors on this line
html.DropDownList("SomeList", list, "Simple List")
导致此错误:
DataBinding: 'System.String' does not contain a property with the name 'Key'.
我已经试过了:
Dim list As SelectList = New SelectList(MyOptions.[Select](Function(x) New With {Key .Value = x.Key, Key .Text = x.Value}), "Value", "Text") ' errors on this line
html.DropDownList("SomeList", list, "Simple List")
导致此错误:
Public member 'Key' on type 'String' not found.
编辑
我明显简化了上面的例子,有点简单。
在我实际代码的某个地方,字典(字符串,字符串)正在转换为 KeyValuePair(字符串,对象)。
实际代码:
在class中我定义了这个public属性:
Public InputFilters As List(Of Dictionary(Of String, Object)) = Nothing ' stores the details of all the added filter types to be rendered
在同一个 class 中,我有这个方法:
Private Sub _Add(ByVal type As String, ByVal labelText As String, ByVal id As String, ByVal defaultValue As Object, ByVal additionalInputClass As String, ByVal placeholder As String, ByVal arrValues As String(), ByVal dictValues As Dictionary(Of String, String), ByVal checked As Boolean)
Dim normalizedValues As New Dictionary(Of String, String)
If dictValues.Count > 0 Then
For Each key As String In dictValues.Keys
normalizedValues.Add(key, dictValues(key))
Next
End If
If arrValues.Count > 0 Then
For Each val As String In arrValues
normalizedValues.Add(val, val)
Next
End If
Dim d = New Dictionary(Of String, Object) From {
{"type", type},
{"labelText", labelText},
{"id", id},
{"defaultValue", defaultValue},
{"additionalInputClass", additionalInputClass},
{"checked", checked}, {"placeholder", placeholder},
{"commonInputClass", CommonInputClass},
{"idPrefix", IdPrefix}}
d.Add("Values", normalizedValues) ' breakpoint and watch here shows d("Values") as type Dictionary(Of String, String)
InputFilters.Add(d) ' breakpoint and watch here shows InputFilters(0)("Values") as type KeyValuePair(Of String, Object)
End Sub
注意最后两行的注释,因为这是从字典到 KeyValuePair 的转换发生的地方。
我在剃须刀视图页面中这样做:
@For Each Filter As Dictionary(Of String, Object) In Model.InputFilters
@Html.BuildFilterInput(Filter)
Next
BuildFilterInput 的 HTML 辅助代码如下所示:
<Extension()>
Public Function BuildFilterInput(ByVal html As HtmlHelper, ByVal Filter As Dictionary(Of String, Object)) As IHtmlString
Dim id As String = (Filter("idPrefix") & Filter("id")).ToString()
Dim Result As String = ""
Result &= "<div class=""col-md-6 col-lg-4 report-filter-item-wrapper"">" & vbCrLf
Result &= " <div class=""form-group"">" & vbCrLf
Result &= " " & html.Label(id, Filter("labelText").ToString).ToString() & vbCrLf
Result &= " "
Select Case Filter("type")
Case "text"
Result &= html.TextBox(id, Filter("defaultValue").ToString(), New With {Key .name = id, Key .[class] = "form-control" & If(Filter("additionalInputClass").ToString = "", "", " " & Filter("additionalInputClass").ToString()) & If(Filter("commonInputClass").ToString() = "", "", " " & Filter("commonInputClass").ToString()), Key .placeholder = If(Filter("placeholder").ToString() = "", "", Filter("placeholder").ToString())}).ToString() & vbCrLf
Case "select"
Dim list As SelectList = New SelectList(Filter.Values, "Key", "Value", "ME") ' this is where it blows up because Filter.Values is not a dictionary !!!!!!
Result &= html.DropDownList(id, list, "What is this").ToString() & vbCrLf
End Select
Result &= " </div>" & vbCrLf
Result &= "</div>" & vbCrLf
Return html.Raw(Result)
End Function
卫生部!这是语法错误
我正在使用 Filter.Values 试图访问 "Values" 键,我应该使用 Filter("Values").