简体   繁体   中英

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

Working through Pro ASP.NET MVC book and I got the following code snippet that appears in an aspx (view) page...

<%= 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"); %>

Can someone help me convert this to vb.net equivalent.

Any ideas anyone?

EDIT
MORE DETAILS
Here is the whole code snippet. Not sure it will help but here it goes.

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

Any help appreciated. I REALLY can't figure this one out.

Seth

you can do this with some fairly verbose VB.NET code:


     <%
        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")%>

The trick is to read the function arguments and supply the appropriate parameters. Sometimes we get used to the more arcane syntaxes and forget about simple variable declarations and assignments. The DropDownList expects an IEnumerable of SelectListItem's as the second argument so that's what we give it. Each SelectListItem should probably have it's value and text fields supplied. What I don't understand is why the SelectListItem's constructor does not supply those two items (plus perhaps a "selected" boolean.)

Try this:

<%  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"))
%>

My VB is pretty weak. I'm sure someone has a better answer.

Don't overcomplicate:

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

Here is what I think you are really looking for. This is as close to the exact translation in my opinion...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM