TheTwoArmedMan
TheTwoArmedMan

Reputation: 135

Anchor link not directing to controller method

I have an anchor <a> element within a cell of a <table>, leading to an action within my app. When I click the link in the browser, the event app goes to the route that I expect it to (Admin/ManageCustomers/ViewPayments/1), but the action doesn't get called (I've put a breakpoint on the expected method, but it doesn't get hit). I've double- and triple-checked the names of the controller and action, and compared it to other places in my app where <a> elements inside of <table>s that do work, and I can't figure out why this one is behaving differently.

If it matters at all, both the view and the controller are inside the "Admin" Area, but I think that would still work (I know that the asp-area tag for the anchor probably isn't necessary, since both the view and the model are in the same Area, but I was just trying everything I could think of at that point).

Controller:

namespace Shop.Areas.Admin.Controllers
{
  [Authorize(Roles =Constants.ROLE_ADMIN)]
  [Area(Constants.ROLE_ADMIN)]
  public class ManageCustomers : Controller
  {
   
    ...
    // other stuff
    ...

    [HttpPost]
    public IActionResult ViewPayments(int id) // This is the method I want to call
    {
      ....
    }
  }
}

View:

...
@* stuff *@
...

<div class="container-fluid">
  <main>
      <table class="table table-bordered table">
        <tr>
          <th>Name</th>
          <th>Balance</th>
          <th></th>
          <th></th>
          <th></th>
        </tr>
        @foreach (Customer customer in Model)
        {          
          ...            
          @* stuff *@
          ...
          <tr>
            <td>
              @* This is the element that isn't working right *@
              <a asp-area="Admin" 
                  asp-controller="ManageCustomers" 
                  asp-action="ViewPayments" 
                  asp-route-id="@customer.CustomerId"  @*From the model*@
                  class="text-center">
                View Payment History
              </a>
            </td>
            ...
            @* stuff *@
            ...
          </tr>
        }
      </table>
  </main>
</div>

Upvotes: 1

Views: 460

Answers (1)

devlin carnate
devlin carnate

Reputation: 8591

The anchor and url it leads to are GET requests. The method you're showing us in your Controller is expecting POST. You need to either change the method to accept GET or create a GET method.

Upvotes: 2

Related Questions