【问题标题】:How to implement htmlAttributes MVC for DropDownListFor using VB如何使用VB为DropDownListFor实现htmlAttributes MVC
【发布时间】:2012-12-27 02:48:42
【问题描述】:

我的 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。

谢谢大家。

【问题讨论】:

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


【解决方案1】:

我不知道为什么只在包含属性对象时出现错误,但我认为问题在于 ViewData("Questions") 返回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 类似的东西

【讨论】:

  • 谢谢我昨天看到了那个页面(链接),但是当我做 try.cast 时,下拉列表没有列出项目详细信息,而是显示了 seletlistitem 本身。我在原始问题中添加了更多代码,显示了我如何实例化下拉列表,如果这有助于为我指明正确的方向。顺便说一句……它确实修复了样式问题!谢谢
【解决方案2】:

原始问题

Html.DropDownListFor 需要 IEnumerable&lt;SelectListItem&gt;,因此您需要将 objectViewData 转换为 SelectList

用于编辑

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 2017-05-08
    • 1970-01-01
    • 1970-01-01
    • 2020-07-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多