Luca Celenza Luca Celenza - 1 year ago 76
C# Question

Ambiguous action methods with different HttpMethod

I'm experiencing a weird behaviour, at least to me. I written two methods within a controller with apparently different signatures:

public async Task<ActionResult> Add(long walletId)

public async Task<ActionResult> Add(AddTransactionViewModel model)

The thing is every time I try to call the POST method using Ajax.BeginForm the GET method (the first) gets called.

@using (Ajax.BeginForm("Add", "Transactions",
new AjaxOptions() { HttpMethod = "POST" })

Now, why this is happening? Of course if I change the name of the GET method say to AddTransaction the code works, but I want to understand why it doesn't as it is.

Answer Source

This is because BeginForm uses GetVirtualPath internally to get the url from the route table. The first link is added to the route table in your example.

Simply editing the POST method with the following should do the trick:

public async Task<ActionResult> Add(AddTransactionViewModel model)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download