4

related: MVC3 Razor using Html.BeginForm problem

When I make an HTML form for MVC 3/VB with the Razor engine, I would expect to be able to do it like this:

@Using Html.BeginForm("Action", "Controller")
    <fieldset>
        @* Other form code and values *@
    </fieldset>
End Using

But if I do that I get "BC32035: Attribute specifier is not a complete statement. Use a line continuation to apply the attribute to the following statement." I need to add an @ character before the opening tag to avoid this error. Can someone explain why?

4 Answers 4

3

When using Razor with C#, what you describe is possible, because parser can determine the transition from code to markup because of the explicit '<' characters in html is not a valid C# token. VB.NET supports inline XML directly in code, so the Razor parser cannot determine that you have transitioned back to markup, so you have to be more explicit.

Sign up to request clarification or add additional context in comments.

Comments

3

I believe in Razor beginform should be like this, ie with curly braces:

@using (Html.BeginForm())
{
    @* Other code here *@
}

Comments

3

I don't use VB.NET, but I think you should do this:

@Using Html.BeginForm("Action", "Controller")
    @:<fieldset>
        @* Other form code and values *@
    @:</fieldset>
End Using

With VB.NET's first-class supports for XML, it treats tags as XML, hence it treats fieldset(any HTML tags for that matter) as XML too; and XML as being part of language of VB.NET, it will run counter to Razor's parser

A quick trip on VS2010 using ASP.NET MVC for VB.NET, this would suffice:

@Using Html.BeginForm("Action", "Controller")
    @<fieldset>
        @* Other form code and values *@
    </fieldset>
End Using

VB.NET's language literal XML support run afoul of Razor's parser, just prevent it with adding extra @ or @:

3 Comments

What does the extra @ or @: do in that code? Is it some kind of escape character?
More or less yes. It signals razor that you are escaping from programming language's (e.g. C#, VB.NET) syntax. There's no extra nudging needed for C#, as the symbol < is not used by it, and as such, Razor can also use the < as an escaper for C#; there's no such privilege in VB.NET, in VB.NET, anything that starts with < is an XML literal, so for this kind of scenario, you need to use Razor's @ to escape the VB.NET's syntax
Another example of Razor's nuance: ienablemuch.com/2011/08/razor-tag-nuances.html
1

For multi-line support, as pointed out in Using in Razor VB.net MVC not work as expected, you can use @<text>...</text> to switch back to "text mode" and output html like this:

@Using Html.BeginForm("Action", "Controller")
    @<text>
      <fieldset>
        * Other form code and values *
      </fieldset>
    </text>
End Using

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.