通过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>
任何帮助表示赞赏。我真的无法想出这个。
赛斯
答案 0 :(得分: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 :(得分: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非常弱。我相信有人会有更好的答案。
答案 2 :(得分: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>
答案 3 :(得分: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")%>
答案 4 :(得分: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" />