Recommand · October 14, 2021 0

HTTP ERROR 405 | [HttpPost] ASP.NET MVC Core 3.1

so here’s the thing :

I have two pages,

  • One with a list of suppliers
  • If I click on one of the suppliers I am supposed to get to the second page which contains all the users of the selected supplier.

Here is my code,

Page SelectSupplier :

var link = '@Url.Action("UserManagement", "Home", new { GB = @ViewBag.GB, id = "code" })';

link = link.replace("code", listSuppliers[i].Item2);

suppliersTable.row.add([
  "<form action='" + link + "'><button type='submit' class='btn btn-primary'>" + listSuppliers[i].Item3 + " account(s)</button></form>" //Number of accounts, clickable to see all accounts
]).draw(false);

Controller :

[HttpPost]
public ActionResult UserManagement(string GB, string id)
{...
return View();
}

The problem is that when I click on my supplier, the page UserManagement displays: "This page isn’t working if the problem continues contact the site owner HTTP Error 405"

I really don’t get it, can anybody help me ?

Why do you want to create a form to go to a link? Why not just build a GET URL with a route or query string?

Anyway, it’s an MVC feature to make forms POST by default. If you don’t specify the method in HTML, then it’s a GET. You could’ve seen this in F12.