2009-08-04 49 views
1

通過Pro ASP.NET MVC書籍的工作,我得到以下代碼片段出現在aspx(view)頁面中...如何呈現以下asp.net mvc c#代碼片段作爲vb.net代碼

<%= Html.DropDownList ("WillAttend",new[] 
{ 
new SelectListItem { Text = "Yes, I'll be there",Value=bool.TrueString}, 
new SelectListItem { Text = "No, I can't come",Value=bool.FalseString} 
},"Choose an option"); %> 

有人可以幫助我將其轉換爲vb.net等效。

任何想法的人?

編輯
更多詳情
這裏是整個代碼段。不知道它會有幫助,但它在這裏。

<body> 
    <h1>RSVP</h1> 

    <% using(Html.BeginForm()) { %> 
     <p> Your name: <%= Html.TextBox("Name") %></p> 
     <p> Your email: <%= Html.TextBox("Email") %></p> 
     <p> Your phone: <%= Html.TextBox("Phone") %></p> 
     <p> 
      Will you attend? 
      <%= Html.DropDownList ("WillAttend",new[] 
       { 
       new SelectListItem { Text = "Yes, I'll be there",Value=bool.TrueString}, 
       new SelectListItem { Text = "No, I can't come",Value=bool.FalseString} 
       },"Choose an option") %> 
     </p> 
     <input type="submit" value="Submit RSVP" /> 
    <% } %> 
</body> 

任何幫助表示讚賞。我真的無法弄清楚這一點。

賽斯

回答

2

你可以用一些比較詳細的VB.NET代碼做到這一點:


    <% 
     Dim lst As New List(Of SelectListItem) 

     Dim item1 As New SelectListItem 
     item1.Text = "Yes, I'll be there" 
     item1.Value = Boolean.TrueString 
     lst.Add(item1) 

     Dim item2 As New SelectListItem 
     item2.Text = "No, I can't come" 
     item2.Value = Boolean.FalseString 
     lst.Add(item2) 
    %> 
    <%=Html.DropDownList("WillAttend", lst, "Choose an option")%> 

關鍵是要讀函數的參數,並提供相應的參數。有時我們習慣於更神祕的語法,忘記簡單的變量聲明和分配。 DropDownList需要一個IEnumerable的SelectListItem作爲第二個參數,所以這就是我們給它的。每個SelectListItem可能應該提供它的值和文本字段。我不明白的是爲什麼SelectListItem的構造函數不提供這兩個項目(加上也許是一個「選定」布爾值。)

1

試試這個:

<% Dim listItems As SelectListItem() = { _ 
      New SelectListItem() With {.Text = "Yes, I'll be there", .Value = Boolean.TrueString}, _ 
      New SelectListItem() With {.Text = "No, I can't come", .Value = Boolean.FalseString} _ 
      } 

      Response.Write(Html.DropDownList("WillAttend", listItems, "Choose an option")) 
%> 

我的VB是相當薄弱。我相信有人有更好的答案。

+0

謝謝你的回答......讓你在一分鐘知道是否奏效。 對於記錄...在發佈我的問題之前,我嘗試將代碼轉換爲http://www.developerfusion.com/tools/convert/vb-to-csharp/ Seth – 2009-08-04 03:48:53

+0

爲了記錄...那沒用。 [「WillAttend」,New()]在New之後有一個錯誤波形線(反正叫什麼?),表示Type Expected。 New NewList中的[New SelectListItem]在語法錯誤的新單詞下有一個波形。所有的數組加載器({})都有錯誤,表明語法錯誤。與選擇一個選項字符串相同。 – 2009-08-04 03:54:26

+0

嘗試建設。也嘗試發佈周圍的HTML代碼。我發現寫入時調試器有點吸引人的查看頁面,尤其是如果它在一個標籤內(不知道爲什麼會這樣,但是...) – 2009-08-04 06:07:51

1

不要過於複雜:

<p>Will you attend?</p> 
<select id="WillAttend"> 
    <option>Select an option</option> 
    <option value="true">Yes, I'll be there</option> 
    <option value="false">No, I can't come</option> 
</select> 
+0

您的選擇也適用,所以我upvoted你的答案。這很好,很簡單。 是否有任何理由不使用您的簡單方法。基於代碼的解決方案是否具有任何優勢。老實說,我認爲只要沒有缺點,我就更喜歡簡單。 Seth – 2009-08-05 13:28:21

1

這是我認爲你真正想要的。這是因爲接近於準確的翻譯在我看來......

 <%: Html.DropDownListFor(Function(x) x.WillAttend, New List(Of SelectListItem) From _ 
     { 
      New SelectListItem With {.Text = "Yes, I'll be there", .Value = Boolean.TrueString}, 
      New SelectListItem With {.Text = "No, I can't come", .Value = Boolean.FalseString} 
     }, "Choose an option")%> 
0
@Using (Html.BeginForm()) _ 
     @<p>Your name: @Html.TextBoxFor(Function(x) x.Name) </p> _ 
     @<p>Your email: @Html.TextBoxFor(Function(x) x.Email) </p> _ 
     @<p>Your phone: @Html.TextBoxFor(Function(x) x.Phone) </p> _ 
     @<p> 
      Will you attend? 
      @Html.DropDownListFor(Function(x) x.WillAttend, {New SelectListItem() With {.Text = "Yes, I'll be there", .Value = Boolean.TrueString}, _ 
        New SelectListItem() With {.Text = "No, I can't come", .Value = Boolean.FalseString}}, _ 
       "Choose an option")  
     </p> 
    End Using 
     <input type="submit" value="Submit RSVP" />