views:

401

answers:

3

Hello,

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

A: 

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.

womp
Thanks for you answer...let you know in a min if that worked.For the record...before posting my question I did try to convert the code at http://www.developerfusion.com/tools/convert/vb-to-csharp/Seth
Seth Spearman
For the record...that did not work. ["WillAttend", New()] has an error squiggly line (what are those called anyway?) after New that says Type Expected. The [New SelectListItem] has a squiggly under New word that says syntax error. All of the array loaders ({}) have errors squiggles that Syntax error. Same with Choose an Option string.
Seth Spearman
Try building. Also try posting the surrounding HTML code. I've found that the write-time debugger kinda sucks in view pages, especially if it's inside a tag (not sure why this would be, but...)
James S
+1  A: 

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>
Jason
Your option worked as well so I upvoted your answer. And it is simple which is good. Are there any reasons to NOT use your uncomplicated method. Does the code based solution offer any advantages. Honestly I think I prefer simple every time as long as there are no downsides.Seth
Seth Spearman
well, with this version, there's no processing. it's just straight html, so it's probably faster, possibly negligbly tho. also less code :)
Jason
+1  A: 

Hello friend, 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.)

Thanks. Did the trick. Seth
Seth Spearman