2009-08-04 54 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" />