harryjunior176651
harryjunior176651

Reputation: 21

Core razor beginform html tag wrong

Good afternoon,

I am working on a simple form in ASP.NET Core 3.1, and I am struggling to understand beginform.

I have a search box on one page (in this case /Index), and using GET (a requirement) when someone types their search in and presses the search button, the Action needs to go off to another page (in this case /Listing )

In the razor form page, if I use : -

@using (Html.BeginForm("Listing", "", FormMethod.Get))
{
}

..... but I get this HTML created : -

<form action="/?action=Listing" method="get">
</form>

What I would expect to see is : -

<form method="get" action="/Listing">
</form>

So what am I doing wrong with my BeginForm above to be getting the wrong HTML created for the tag please?

I have tried numerous answers found elsewhere but they are for MVC and I am using Razor pages only not MVC.

Thank you in adavnce if you can help?

Harry

Upvotes: 2

Views: 760

Answers (1)

Rosco
Rosco

Reputation: 2514

Html.BeginForm is the older way from MVC. It's parameters are Action and Controller e.g.

Html.BeginForm("AddNewCustomer", "Customer", FormMethod.Post)

Posts to CustomerController calling the AddNewCustomer method.

If you are using the new Razor pages then you write the html directly and add attributes that start with asp-.

e.g. Build a Form in Razor pages

If you want to set the page where the form posts to the Listing.cshtml page would look like this:

<form method="get" asp-page="./Listing">
</form>

Note that asp-page is a path to a Razor page file relative to the current file. Not a Url link.

Upvotes: 1

Related Questions