c# - 如何将以下 asp.net mvc c# 代码片段呈现为 vb.net 代码

标签 c# asp.net asp.net-mvc vb.net

通过 Pro ASP.NET MVC 书工作,我得到了以下出现在 aspx( View )页面中的代码片段...

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

感谢任何帮助。我真的想不通这个。

赛斯

最佳答案

您可以使用一些相当冗长的 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 期望 SelectListItem 的 IEnumerable 作为第二个参数,所以这就是我们给它的。每个 SelectListItem 都应该提供它的值和文本字段。我不明白的是为什么 SelectListItem 的构造函数不提供这两项(可能加上一个“选定的” bool 值。)

关于c# - 如何将以下 asp.net mvc c# 代码片段呈现为 vb.net 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1225606/

相关文章:

.net - URL 中的 ASP.NET aspxerrorpath

asp.net - 用户更改密码后,旧密码和新密码均有效

c# - 从网络中的 UNC 路径加载图像并隐藏该路径

c# - LINQ 查询出现问题 : Select first task from each goal

c# - 如何使用动态创建的 jquery 获取文本框的值?

javascript - 如何将整套模型附加到formdata并在MVC中获取

c# - Entity Framework ICollection 导航属性仅在测试时为 null

javascript - AngularJS:条件 ng 类,其函数触发荒谬的金额

c# - 在 Entity Framework 中使用存储过程的乐观并发

c# - Linq to Entity 比较字符串忽略空格