簡體   English   中英

如何將以下asp.net MVC C#代碼片段呈現為vb.net代碼

[英]How to render the following asp.net mvc c# code snippet as vb.net code

通過Pro ASP.NET MVC本書,我得到了顯示在aspx(視圖)頁面中的以下代碼段...

<%= 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>

任何幫助表示贊賞。 我真的不知道這一點。

賽斯

您可以使用一些相當冗長的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期望將SelectListItem的IEnumerable作為第二個參數,因此我們可以提供它。 每個SelectListItem可能應該提供它的值和文本字段。 我不明白的是為什么SelectListItem的構造函數不提供這兩個項目(可能還有“選擇的”布爾值)。

嘗試這個:

<%  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很弱。 我相信有人會提供更好的答案。

不要過於復雜:

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

這是我認為您真正想要的。 我認為這與確切的翻譯非常接近...

        <%: 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")%>
@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" />

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM