Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc VB.NET中Razor视图引擎的怪癖_Asp.net Mvc_Vb.net_Visual Studio 2010_Razor - Fatal编程技术网

Asp.net mvc VB.NET中Razor视图引擎的怪癖

Asp.net mvc VB.NET中Razor视图引擎的怪癖,asp.net-mvc,vb.net,visual-studio-2010,razor,Asp.net Mvc,Vb.net,Visual Studio 2010,Razor,我刚刚下载了MVC3.0RC,我很高兴开始使用它,尤其是Razor视图引擎。然而,由于这里有一些固执己见的人,我们使用VB.NET而不是C# 当我开始尝试时,我注意到了一些怪癖。如果使用CSHTML创建Razor视图,可以编写如下代码: @foreach(string genreName in Model.Genres) { <li>@genreName</li> } @For Each genreName As String In Model.Genres

我刚刚下载了MVC3.0RC,我很高兴开始使用它,尤其是Razor视图引擎。然而,由于这里有一些固执己见的人,我们使用VB.NET而不是C#

当我开始尝试时,我注意到了一些怪癖。如果使用CSHTML创建Razor视图,可以编写如下代码:

@foreach(string genreName in Model.Genres)
{
    <li>@genreName</li>
}
@For Each genreName As String In Model.Genres
    @:<li>@genreName</li>
Next
如果我没有它,我会得到一个运行时错误。而且,
标记似乎不起作用


有人知道这里发生了什么或者是否有解决方法吗?

我刚刚在ASP.NET MVC 3 RC中的VBHTML视图中尝试了这个方法,它似乎工作得很好:

<ul>
@For Each i As Integer In Enumerable.Range(0, 5)
    @:<li>@i</li>
Next
</ul>
    @对于可枚举范围(0,5)中的整数形式的每个i @:
  • @i
  • 下一个
它呈现此标记:

<ul>
    <li>0</li>
    <li>1</li>
    <li>2</li>
    <li>3</li>
    <li>4</li>
</ul>
  • 0
  • 一,
  • 二,
  • 三,
  • 四,

我想说,Vb.net中需要它的原因是Vb允许xml元素内联,而c#不允许

Dim xmlMarkup = <someElement>Values</span>
Dim xmlMarkup=值
因此,vb的自然解析器的行为必须与c不同,因此您必须告诉解析器使用
@
返回html。您可以使用
@
@:

更新:

你可以用@li@genreName/li没有:因为您的HTML标记是自动关闭的,如果不是,那么您需要使用@:但是您没有,这在Gabe提供的链接中得到了澄清。而且,
@
也可以工作!我不想让人们认为他们需要一直使用@:因为他们不需要,如果是这样的话,我会告诉安德鲁·纳瑟斯这个硬核:) 结束更新

下面是vb razor的基本语法

@character启动内联表达式、单语句块和多语句块:

?
<!-- Single statement blocks  -->

@Code  Dim total = 7  End Code

@Code  Dim myMessage = "Hello World" End Code

 
<!-- Inline expressions -->
<p>The value of your account is: @total </p>

<p>The value of myMessage is: @myMessage</p>

        
     
<!-- Multi-statement block -->

@Code

    Dim greeting = "Welcome to our site!"

    Dim weekDay = DateTime.Now.DayOfWeek

    Dim greetingMessage = greeting & " Today is: " & weekDay.ToString()

End Code

<p>The greeting is: @greetingMessage</p>
?
@代码尺寸总计=7个结束代码
@代码Dim myMessage=“Hello World”结束代码
 
您的帐户价值为:@total

myMessage的值为:@myMessage

              @代码 Dim greeting=“欢迎访问我们的网站!” Dim weekDay=DateTime.Now.DayOfWeek Dim greetingMessage=greeting&“今天是:”&weekDay.ToString() 结束代码 问候语是:@greetingMessage


所以它应该可以工作,只需关闭每行末尾的代码模式并返回HTML,这将避免您使用@:-hunt to ellon,我认为他试图不必使用@:

,正如前面在评论中提到的,但没有明确显示;在文本标记前面加上@可以解决以下问题:

@For Each genreName As String In Model.Genres
    @<text>
        <li>@genreName</li>
    </text>
Next
@对于每种类型,重命名为Model.Genres中的字符串
@
  • @更名
  • 下一个
    这与OP使用的代码不同,但我将一些C代码从一本MVC书籍转换成VB.NET,并陷入了混合内嵌HTML和VB代码的困境。这是原文C#:

    @使用(Html.BeginForm()){
    @Html.ValidationSummary()
    您的姓名:@Html.TextBoxFor(x=>x.name,new{@class=“form control”})

    您的电子邮件:@Html.TextBoxFor(x=>x.email,new{@class=“form control”})

    您的手机:@Html.TextBoxFor(x=>x.phone,新{@class=“form control”})

    你会参加吗? @Html.DropDownListFor(x=>x.willattain,new[]{ 新建SelectListItem(){Text=“是的,我会在那里”, Value=bool.TrueString}, 新建SelectListItem(){Text=“不,我不能来”, Value=bool.false字符串} },“选择一个选项”,新建{@class=“form control”})

    }
    以下是在VB中表示它的不同方式:

    @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() {New SelectListItem() With {.Text = "Yes", .Value = Boolean.TrueString}, New SelectListItem() With {.Text = "No", .Value = Boolean.FalseString}})
        @:</p>
    End Using
    
    @Using Html.BeginForm()
        @<text>
            <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() {New SelectListItem() With {.Text = "Yes", .Value = Boolean.TrueString}, New SelectListItem() With {.Text = "No", .Value = Boolean.FalseString}})
            </p>
        </text>
    End Using
    
    @code
        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() {New SelectListItem() With {.Text = "Yes", .Value = Boolean.TrueString}, New SelectListItem() With {.Text = "No", .Value = Boolean.FalseString}})
            @:</p>
        End Using
    End Code
    
    @code    
        Using Html.BeginForm()
            @<text>
                <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() {New SelectListItem() With {.Text = "Yes", .Value = Boolean.TrueString}, New SelectListItem() With {.Text = "No", .Value = Boolean.FalseString}})
                </p>
            </text>
        End Using
    End Code
    
    @使用Html.BeginForm()
    @:您的姓名:@Html.TextBoxFor(函数(x)x.name)

    @:您的电子邮件:@Html.TextBoxFor(函数(x)x.email)

    @:您的手机:@Html.TextBoxFor(函数(x)x.phone)

    @:你会参加吗? @Html.DropDownListFor(函数(x)x.willAttain,新SelectListItem(){New SelectListItem()和{.Text=“Yes”,.Value=Boolean.TrueString},新SelectListItem()和{.Text=“No”,.Value=Boolean.FalseString}) @:

    终端使用 @使用Html.BeginForm() @ 您的姓名:@Html.TextBoxFor(函数(x)x.name)

    您的电子邮件:@Html.TextBoxFor(函数(x)x.email)

    您的手机:@Html.TextBoxFor(Function(x)x.phone)

    你会参加吗? @Html.DropDownListFor(函数(x)x.willAttain,新SelectListItem(){New SelectListItem()和{.Text=“Yes”,.Value=Boolean.TrueString},新SelectListItem()和{.Text=“No”,.Value=Boolean.FalseString})

    终端使用 @代码 使用Html.BeginForm() @:您的姓名:@Html.TextBoxFor(函数(x)x.name)

    @:您的电子邮件:@Html.TextBoxFor(函数(x)x.email)

    @:您的手机:@Html.TextBoxFor(函数(x)x.phone)

    @:你会参加吗? @Html.DropDownListFor(函数(x)x.willAttain,新SelectListItem(){New SelectListItem()和{.Text=“Yes”,.Value=Boolean.TrueString},新SelectListItem()和{.Text=“No”,.Value=Boolean.FalseString}) @:

    终端使用 结束代码 @代码 使用Html.BeginForm() @ 您的姓名:@Html.TextBoxFor(函数(x)x.name)

    您的电子邮件:@Html.TextBoxFor(函数(x)x.email)

    您的手机:@Html.TextBoxFor(Function(x)x.phone)

    你会参加吗? @Html.DropDownListFor(函数(x)x.willAttain,新SelectListItem(){New SelectListItem()和{.Text=“Yes”,.Value=Boolean.TrueString},新SelectListItem()和{.Text=“No”,.Value=Boolean.FalseString})

    终端使用 结束代码
    从这一点可以明显看出,当您需要在代码块中包含内联HTML时,您需要在每一行前面加一个
    @:
    ,或者在HTML中加一个
    @
    块。当您使用
    @code时,这一点也很明显适用<
    
    @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() {New SelectListItem() With {.Text = "Yes", .Value = Boolean.TrueString}, New SelectListItem() With {.Text = "No", .Value = Boolean.FalseString}})
        @:</p>
    End Using
    
    @Using Html.BeginForm()
        @<text>
            <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() {New SelectListItem() With {.Text = "Yes", .Value = Boolean.TrueString}, New SelectListItem() With {.Text = "No", .Value = Boolean.FalseString}})
            </p>
        </text>
    End Using
    
    @code
        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() {New SelectListItem() With {.Text = "Yes", .Value = Boolean.TrueString}, New SelectListItem() With {.Text = "No", .Value = Boolean.FalseString}})
            @:</p>
        End Using
    End Code
    
    @code    
        Using Html.BeginForm()
            @<text>
                <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() {New SelectListItem() With {.Text = "Yes", .Value = Boolean.TrueString}, New SelectListItem() With {.Text = "No", .Value = Boolean.FalseString}})
                </p>
            </text>
        End Using
    End Code