Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@Html.BeginForm syntax in Visual Basic

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?

like image 550
James Balentine Avatar asked Jan 14 '16 22:01

James Balentine


People also ask

What is HTML BeginForm ()?

BeginForm(HtmlHelper) Writes an opening <form> tag to the response. The form uses the POST method, and the request is processed by the action method for the view. BeginForm(HtmlHelper, String, String, Object, FormMethod, Object)

What is difference between HTML BeginForm and ajax BeginForm?

BeginForm() will create a form on the page that submits its values to the server as a synchronous HTTP request, refreshing the entire page in the process. Ajax. BeginForm() creates a form that submits its values using an asynchronous ajax request.

How do you pass a route value in HTML BeginForm?

The value will only be added as query string values if its FormMethod. Get (and you can remove the route values from the BeginForm() method). But what is the point of this - they are hidden inputs, not editable values.


1 Answers

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>
like image 159
Sam Axe Avatar answered Oct 19 '22 19:10

Sam Axe