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ヘルパーはなく、helper(?)メソッドのデフォルトの組み込み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コントローラーのReagisterACTION内にあります。

'-> 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()

そしてこれがselectlistitemを出力する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

つまり、本質的に、私は最初に選択リストを渡すので、別の選択リストにキャストしませんでした-コメントで言ったように、スタイルは修正されますが、別の問題が残ります:下の写真は私が何を示しているかを示しています得る..

ここに画像の説明を入力してください

閉鎖

私がこれを理解するのを手伝ってくれたすべての人に感謝します、その人の答えが私が問題を特定するのを助けたので、私はそれらの1つを答えとしてマークしました、それは必ずしも他の答えが間違っていたことを意味しません、それはおそらく何よりも私の理解の欠如です。

この問題を解決するために、ビューデータを渡す方法をもう一度確認しました。SELECTLISTを渡すことはできましたが、SELECTLIST.ITEMSコレクションを渡すことに戻り、VIEWで、渡されたデータを選択リストに再スコープしました。データはselectlistオブジェクトの代わりに表示され、HTML.DROPDOWNLISTFORメソッドにHTMLATTRIBUTESが追加されています。

みんな、ありがとう。

4

2 に答える 2

3

属性オブジェクトを含めたときにのみエラーが発生する理由はわかりませんが、問題は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で似たようなもの

于 2013-01-12T19:06:03.593 に答える
2

元の質問について

Html.DropDownListForが必要なので、からにIEnumerable<SelectListItem>キャストする必要があります。objectViewDataSelectList

編集用

のオーバーロードされたコンストラクターを使用SelectListします。ここで、dataValueFieldおよびdataTextField(最終的にはselectedItem)

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