2013-01-12 63 views
3

實施htmlAttributes MVC的DropDownListFor我有我的Register.Aspx View頁面下面的代碼如何使用VB

<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 Helpers,並且假定它應該工作而無需爲helper(?)方法擴展默認的內置MVC dropdownlist。

出現的錯誤...

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> 

我想要了解的是...

我需要延長HTMLHELP呃下拉列表? 我需要在我的控制器代碼中添加一些東西嗎?

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

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

在正確的方向或引用鏈接任何指針將是有益的。 謝謝

編輯

下面這段代碼是在賬戶控制器我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 

所以輸出selectlistitem,我傳遞一個選擇列表的一審所以這就是爲什麼我沒扔在別的選擇列表的 - 雖然像我在評論它確實解決了造型,但給我留下了另一個問題重新說:下面的照片顯示了我得到..

enter image description here

Closing

謝謝大家幫我解決這個問題,我已經標記了其中一個答案,因爲人們的答案幫助我指出了問題,它不一定意味着其他答案是錯誤的,它可能是我的缺點理解比什麼都重要。

要解決此問題我再次查看了傳遞viewdata的方式,儘管我通過SELECTLIST傳遞了一個SELECTLIST.ITEMS集合,然後在VIEW中將傳遞的數據重新傳遞給了一個selectlist,現在數據可見而不是選擇列表對象,並且它在HTML.DROPDOWNLISTFOR方法中附加了HTMLATTRIBUTES。

謝謝大家。

+0

也許問題的構造與ViewData的(作爲參數傳遞 「問題」)。檢查http://msdn.microsoft.com/en-us/library/ee834953%28v=vs.108%29.aspx – Brij

回答

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

+0

謝謝我昨天遇到該頁面(鏈接),但是當我做try.cast下降下拉列表不列出項目細節,但inst它顯示了seletlistitem本身。我已經添加了一些更多的代碼給我的原始問題,它顯示了我如何實例化下拉列表,如果這有助於指向正確的方向。順便提一句,雖然......它確實FIX的造型isuue!謝謝 – Zeddy

2

對於原來的問題

Html.DropDownListFor需求IEnumerable<SelectListItem>所以你需要投objectViewDataSelectList

對於編輯

使用重載的SelectList,在那裏你指定dataValueFielddataTextField(最終selectedItem)

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