3

我的 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 格式......

<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 函数

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

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

在此处输入图像描述

关闭

谢谢所有帮助我解决这个问题的人,我已将其中一个标记为答案,因为该人的回答帮助我查明了问题,这并不一定意味着另一个答案是错误的,这可能是我缺乏理解比什么都重要。

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

感谢大家。

4

2 回答 2

3

我不知道为什么只有在包含属性对象时才会出现错误,但我认为问题在于 ViewData("Questions") 返回 an Object,而不是 an 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

于 2013-01-12T19:06:03.593 回答
2

对于原始问题

Html.DropDownListFor需要IEnumerable<SelectListItem>所以你需要objectViewDatato 转换SelectList

用于编辑

使用 的重载构造函数SelectList,在其中指定dataValueFieldand dataTextField(最终selectedItem).

new SelectList(collection, "DataValueFieldName", "DataTextFieldName", "SelectedItemValue");
于 2013-01-12T21:58:13.750 回答