如何使用VB为DropDownListFor实现htmlAttributes MVC

时间:2013-01-12 18:21:38

标签: asp.net-mvc asp.net-mvc-2 asp.net-3.5

我在Register.Aspx查看页面中有以下代码

<div class="FieldDropDown">
  <%: Html.DropDownListFor(Function(m) m.Question, ViewData("Questions"), New With {.style = "margin-bottom: 24px; font-family: Tahoma; font-size: 12pt; color: #333333;"})%>
  <%: Html.ValidationMessageFor(Function(m) m.Question)%>
</div>

问题在于它不断出现错误,我似乎无法确定如何实现代码。

我没有自己定义的任何自定义或额外的HTML帮助程序,并且认为它应该可以正常运行,而无需扩展默认的内置MVC下拉列表以帮助(?)方法。

出现的错误是......

Overload resolution failed because no accessible 'DropDownListFor' can be called without a narrowing conversion:
Extension method 'Public Function DropDownListFor(Of String)(expression As System.Linq.Expressions.Expression(Of System.Func(Of MvcApplication1.RegisterModel, String)), selectList As System.Collections.Generic.IEnumerable(Of System.Web.Mvc.SelectListItem), htmlAttributes As System.Collections.Generic.IDictionary(Of String, Object)) As System.Web.Mvc.MvcHtmlString' defined in 'System.Web.Mvc.Html.SelectExtensions': Argument matching parameter 'selectList' narrows from 'Object' to 'System.Collections.Generic.IEnumerable(Of System.Web.Mvc.SelectListItem)'.
Extension method 'Public Function DropDownListFor(Of String)(expression As System.Linq.Expressions.Expression(Of System.Func(Of MvcApplication1.RegisterModel, String)), selectList As System.Collections.Generic.IEnumerable(Of System.Web.Mvc.SelectListItem), htmlAttributes As System.Collections.Generic.IDictionary(Of String, Object)) As System.Web.Mvc.MvcHtmlString' defined in 'System.Web.Mvc.Html.SelectExtensions': Argument matching parameter 'htmlAttributes' narrows from '<anonymous type> (line 149)' to 'System.Collections.Generic.IDictionary(Of String, Object)'.
Extension method 'Public Function DropDownListFor(Of String)(expression As System.Linq.Expressions.Expression(Of System.Func(Of MvcApplication1.RegisterModel, String)), selectList As System.Collections.Generic.IEnumerable(Of System.Web.Mvc.SelectListItem), htmlAttributes As Object) As System.Web.Mvc.MvcHtmlString' defined in 'System.Web.Mvc.Html.SelectExtensions': Argument matching parameter 'selectList' narrows from 'Object' to 'System.Collections.Generic.IEnumerable(Of System.Web.Mvc.SelectListItem)'.

当我将HTMLATTRIBUTES删除到下面的代码时,它工作正常并且没有出现错误但是DropDownList也没有样式,它看起来很简单,没有我想要的CSS格式... < / p>

<div class="FieldDropDown">
  <%: Html.DropDownListFor(Function(m) m.Question, ViewData("Questions"))%>
  <%: Html.ValidationMessageFor(Function(m) m.Question)%>
</div>

我想要了解的是......

我是否需要为下拉列表扩展htmlhelper? 我是否需要在控制器代码中添加内容?

我的控制器代码如下......

<Required()> _
Public Property Question() As String
  Get
    Return _Question
  End Get
  Set(value As String)
    _Question = value
  End Set
End Property

正确方向的任何指针或参考链接都会有所帮助。 谢谢

修改

以下代码位于ACCOUNT控制器的Reagister ACTION中。

'-> Okay
Item0 = NewQuestion("0", "Please select an option", True)
Item1 = NewQuestion("1", "What was your mothers maiden name?", False)
Item2 = NewQuestion("2", "Where were you born?", False)
Item3 = NewQuestion("3", "What was the name of your first love?", False)
Item4 = NewQuestion("4", "What was the name of your favourite pet?", False)
Item5 = NewQuestion("5", "What is the name of your favourite song?", False)
Item6 = NewQuestion("6", "What is the name of your idol/mentor?", False)
Item7 = NewQuestion("7", "What model of your first car?", False)
Item8 = NewQuestion("8", "What model was your first motorcycle?", False)
Item9 = NewQuestion("9", "Who is your favourite sports-person?", False)
Item10 = NewQuestion("10", "What was your last ACTIVATION KEY?", False)
SQuestions = Nothing
SQuestions = New SelectList({Item0, Item1, Item2, Item3, Item4, Item5, Item6, Item7, Item8, Item9, Item10}, Item0)
Session("Website_Guest") = ThisGuest
ViewData("VisitorName") = ThisGuest.Username
'ViewData("Questions") = New SelectList(SQuestions.Items) <- TRIED IT LIKE THIS TOO
'ViewData("Questions") = New SelectList(SQuestions) <- TRIED IT LIKE THIS TOO
ViewData("Questions") = sQuestions
ViewData("PasswordLength") = MembershipService.MinPasswordLength
Return View()

这是NEWQUESTION函数,它输出一个selectlistitem

Private Function NewQuestion(NewValue As String, NewData As String, NewSelected As Boolean) As SelectListItem
Dim Item As SelectListItem = Nothing
  Item = Nothing
  If Trim(NewValue) <> "" Then
    If Trim(NewData) <> "" Then
        Item = New SelectListItem()
        Item.Value = NewValue
        Item.Text = NewData
        Item.Selected = NewSelected
    End If
  End If
  Return Item
  Item = Nothing
End Function

所以从本质上讲,我在第一个实例中传递了一个选择列表,这就是为什么我没有投入另一个选择列表 - 尽管我在评论中说它确实修复了样式但又留下了另一个问题:下面的照片显示我得到的......

enter image description here

的封闭

谢谢大家帮助我解决这个问题的所有人,我已将其中一个标记为答案,因为人们的回答帮助我查明了问题,它并不一定意味着其他答案是错误的,可能是我缺乏了解更多什么都没有。

解决问题我再次看了我传递viewdata的方式,虽然我DID传递了一个SELECTLIST,但我还原为传递SELECTLIST.ITEMS集合,然后在VIEW中我将传递的数据重新定义为选择列表,现在数据是可见的而不是selectlist对象,它在HTML.DROPDOWNLISTFOR方法中附加了HTMLATTRIBUTES。

谢谢大家。

2 个答案:

答案 0 :(得分:3)

我不知道为什么在包含属性对象时只会出现错误,但我认为问题在于ViewData(“问题”)返回Object,而不是IEnumerable(Of SelectListItem) ,请尝试:

<%: Html.DropDownListFor(Function(m) m.Question,
  TryCast(ViewData("Questions"), SelectList),
  New With {.style = "margin-bottom: 24px; font-family: Tahoma; font-size: 12pt; color: #333333;"}) %>

http://forums.asp.net/t/1466151.aspx/1

的类似内容

答案 1 :(得分:2)

原始问题

Html.DropDownListFor需要IEnumerable<SelectListItem>,因此您需要将objectViewData投射到SelectList

进行修改

使用SelectList的重载构造函数,您可以在其中指定dataValueFielddataTextField(最终selectedItem)

new SelectList(collection, "DataValueFieldName", "DataTextFieldName", "SelectedItemValue");