Razor View Engine Quirks in VB.NET

后端 未结 5 1565
温柔的废话
温柔的废话 2020-12-09 16:41

I just downloaded the MVC 3.0 RC and I\'m excited to start using it, especially the Razor view engine. However, due to a few stick in the mud type people here, we are stuck

相关标签:
5条回答
  • This doesn't use the same code as the OP but I was converting some C# code from an MVC book into VB.NET and got stuck with mixing inline HTML and VB code. This is the original C#:

    @using (Html.BeginForm()) {
        @Html.ValidationSummary()
            <p>Your name: @Html.TextBoxFor(x => x.Name, new { @class = "form-control" }) </p>
            <p>Your email: @Html.TextBoxFor(x => x.Email, new { @class = "form-control" }) </p>
            <p>Your phone: @Html.TextBoxFor(x => x.Phone, new { @class = "form-control" }) </p>
            <p>Will you attend?
            @Html.DropDownListFor(x => x.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", new { @class = "form-control" })
            </p>
    }
    

    and these are the different ways of representing it in 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
    

    What should be obvious from this is that when you need to include inline HTML within a code block you need to either prefix each line with a @: or enclose the HTML with an @<text></text> block. It's also obvious that this applies when you are using @Code ... End Code instead of starting a block of code with @.

    p.s. Note that the @<text></text> tags are not output to the page so they won't interfere with anything

    0 讨论(0)
  • 2020-12-09 17:09

    I would say the reason it's required in Vb.net is vb allows xml elements inline whereas c# does not.

    Dim xmlMarkup = <someElement>Values</span>
    

    Because of this the natural parser for vb must behave differently than c# so you have to tell the parser to escape back to html by using the @. You can use @ and @:.

    0 讨论(0)
  • 2020-12-09 17:09

    UPDATE:

    You can use @li@genreName/li without the : because your HTML tags are self closing, if they weren't then you would need to use @: but you do not, this is clarified in the link provided by Gabe. Also, @<text> will also work! I would hate for people to think they need to use @: all the time because they don't, if this were the case I'd be onto Andrew nurse about this hardcore :) END UPDATE

    Here is the basic syntax for vb razor,

    The @ character starts inline expressions, single-statement blocks, and multi-statement blocks:

    ?
    <!-- 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>
    

    So it should work, just close code mode at the end of the for each line and your back in HTML, this would avoid you using the @: - hunt to ellon, I think he is trying to not have to use @:

    0 讨论(0)
  • 2020-12-09 17:10

    As previously mentioned in comments, but not explicitly shown; prepending the text tag with @ solves the issue:

    @For Each genreName As String In Model.Genres
        @<text>
            <li>@genreName</li>
        </text>
    Next
    
    0 讨论(0)
  • 2020-12-09 17:15

    I just tried this in ASP.NET MVC 3 RC in a VBHTML view and it seems to work fine:

    <ul>
    @For Each i As Integer In Enumerable.Range(0, 5)
        @:<li>@i</li>
    Next
    </ul>
    

    It renders this markup:

    <ul>
        <li>0</li>
        <li>1</li>
        <li>2</li>
        <li>3</li>
        <li>4</li>
    </ul>
    
    0 讨论(0)
提交回复
热议问题