5

I'm using ASP.NET MVC 5 and Visual Studio Express 2013. I have this small form in a C# project:

<body>
    @using (Html.BeginForm())
    {
        <p>Your name: @Html.TextBoxFor(x => x.Name) </p>
        <p>Your email: @Html.TextBoxFor(x => x.Email) </p>
        <p>Your phone: @Html.TextBoxFor(x => x.Phone) </p>
    }
</body>

I tried to translate the code above into VB.NET as shown below:

<body>
    @Using Html.BeginForm()
        <p>Your name: @Html.TextBoxFor(Function(m), m.Name)</p>
        <p>Your email: @Html.TextBoxFor(Function(m), m.Email)</p>
        <p>Your phone: @Html.TextBoxFor(Function(m), m.phone)</p>
    End Using
</body>

I have blue lines under the 'Your' in each "P" element. The hint-help when I hover over the blue line is 'Attribute specifier is not a complete statement. Use a line continuation to apply the attribute to the following statement.'.

When I debug the page, the error is 'BC30201: Expression expected.' on the line where the @Using occurs.

What am I doing wrong?

3
  • P.S. The C# code works as expected. Commented Jan 14, 2016 at 22:08
  • is the @Using followed by @Html.BeginForm() a typo? You might not need two @'s on that line Commented Jan 14, 2016 at 22:14
  • It wasn't exactly a typo. I tried inserting extra @'s at various places to see if I could get the syntax right. I forgot to remove that one when I wrote up the post. I verified that it was not present in my VB code, reran the page, and I'm still getting problems. The error message shifts from the line with the @Using to the following line (the first textbox), and the message is: BC32035: Attribute specifier is not a complete statement. Use a line continuation to apply the attribute to the following statement. Commented Jan 14, 2016 at 23:18

1 Answer 1

10

As @rogerdeuce surmised, you do not need the second @ symbol.

@ is used to switch context from VB code to HTML/text.

So the correct code would look like:

<body>
    @Using Html.BeginForm()
        @<p>Your name: @Html.TextBoxFor(Function(m) m.Name)</p>
        @<p>Your email: @Html.TextBoxFor(Function(m) m.Email)</p>
        @<p>Your phone: @Html.TextBoxFor(Function(m) m.phone)</p>
    End Using
</body>

Or you could use the @<text> shortcut:

<body>
    @Using Html.BeginForm()
        @<text>
        <p>Your name: @Html.TextBoxFor(Function(m) m.Name)</p>
        <p>Your email: @Html.TextBoxFor(Function(m) m.Email)</p>
        <p>Your phone: @Html.TextBoxFor(Function(m) m.phone)</p>
        </text>
    End Using
</body>
Sign up to request clarification or add additional context in comments.

4 Comments

Note that I renamed your expression variable from model to m to avoid naming conflicts with the built-in Model variable.
Thank you. I tried both examples above. When I change the code in my page to one of them, I have a blue line under the comma in the lambda expression, and under the 'm' in m.Name. The blue line under the comma hovers as 'Expression expected', and the blue line under the 'm' hovers as "'m' is not declared. It may be inaccessible due to its protection level." When I debug the page, it aborts with a message of 'BC30201: Expression expected.'
Whoops. Corrected that. There's no comma in lambda expressions.
Doh! You're right! That fixed it! Thank you, @Sam Axe.

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.