2

There is very little code out there that is in VB, and i'm getting stuck all the time. Can someone tell me the VB equivalent of this C# code?

Thx...

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

4 Answers 4

2

Thanks TV for pointing me in the right direction... I struggled with nutting the array Constructer Type in VB - It was right there all the time....

Robert's on page 26 of Steven Sanderson's great book, Pro ASP.NET MVC Framework.

Many thanks.

Gordon

<% 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 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} _
           }, "Choose an option")%>
    </p>
    <input  type="submit" value="Submit RSVP" /> 

<% End Using%> 
Sign up to request clarification or add additional context in comments.

1 Comment

I added an answer showing the same code in Razor syntax for Visual Basic, in case someone finds that helpful.
0

The VB equivalent for your SelectList should be:

Dim yesNo as SelectList = {
    New SelectListItem With { .Text = "Yes, I'll be there", .Value = Boolean.TrueString }, _
    New SelectListItem With { .Text = "No, I can't come", .Value = Boolean.FalseString } _
}

http://www.cynotwhynot.com/blog/post/Does-VBNET-have-Collection-Initializers.aspx

1 Comment

Thx, but how would i use this code in an aspx file? I'm reading a book and i'm trying to follow the example. The example goes like this: <body> <% using(Html.BeginForm()) { %> <p>Your name: <%= Html.TextBox("Name") %></p> <p>Your email: <%= Html.TextBox("Email")%></p> <p>Your phone: <%= Html.TextBox("Phone")%></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") %> <% } %> </body>
0

I was coding the tutorials in the PRO ASP.NET MVC 5 book from Adam Freeman and had the same problem.

The book is in c# and I wanted to code them in VB.

This was what worked for me :

@Html.DropDownListFor(Function(GuestResponse) GuestResponse.WillAttend, New 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} _
                                      },"Choose an option")

Comments

0

This is the same as Gordon's answer above, but in Razor syntax, instead of ASPX syntax, in case that would be helpful to someone. (It was helpful to me:-)

@Using Html.BeginForm()
 @<text>
    <p>Your name: @Html.TextBoxFor(Function(m) m.Name)</p>
    <p>Your email: @Html.TextBoxFor(Function(m) m.Email)</p>
    <p>Your phone: @Html.TextBoxFor(Function(m) m.Phone)</p>
    <p>
        Will you attend?
        @Html.DropDownList("WillAttend", New 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}}, _
                                            "Choose an option")
    </p>
    <input type="submit" value="Submit RSVP" />
</text>
End Using

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.