0

Basically, I have these 2 actions:

[HttpPost]
[ActionName("ListarTodosGrupo")]
public ActionResult ListAllFromGroup(string wildcard = "", int registries = 10)
{
    // ...
}

[HttpPost]
[ActionName("ListarTodosGrupo")]
public ActionResult ListAllFromGroup(string cnpj, string wildcard = "", int registries = 10)
{
    // ...
}

These actions are supposed to be called from an Ajax call. What I am trying to do is to call different actions depending on the Ajax call arguments (data). Eg:

$.ajax({
    url: "/Cliente/ListarTodosGrupo",
    type: "post",
    dataType: "json",
    cache: true,
    data: { // This should fire the first action
        wildcard: $("input#nomeCliente").val(),
        registries: 10
    },
    ...
});

$.ajax({
    url: "/Cliente/ListarTodosGrupo",
    type: "post",
    dataType: "json",
    cache: true,
    data: { // This should fire the second action
        wildcard: $("input#nomeCliente").val(),
        registries: 10,
        cnpj: '02696818000116' 
    },
    ...
});

However, it is not working (only the first action is triggered, despite the number of given params). Is it even possible? How can I do this?

Yves Calaci
  • 1,019
  • 1
  • 11
  • 37
  • Delete the first method. If you don't post a value for `cnpj`, then it will just be `null` –  May 25 '16 at 11:49
  • @StephenMuecke Good point! I have to admit I couldnt come up with this. However, these 2 functions are intended to have different logics. The first one (without the 'cnpj' param), is supposed to find out a valid cnpj value, according to some session value, and call the second action. The second action is supposed to be called from whitchever ajax call for a given (known) 'cnpj' value. – Yves Calaci May 25 '16 at 11:53
  • If its null, perform one action and its not, perform the other action –  May 25 '16 at 11:54

2 Answers2

0

.NET MVC does not take overloaded methods into consideration when invoking actions, only the name of the method and any http-verb attributes (Like HttpPost, HttpGet etc). Therefore only the first method will always be called, no matter what data you sent it.

You need to rethink you solution. The easiest change would be to have a single public method, ListarTodosGrupos, that take all the potienially neccesary data and default the others to null, where applicable ofc). The you create private methods for the different functionality. Based on the data sent in to ListarTodoGropus, you can make your code call the desired private method.

Björn Fyrvall
  • 231
  • 1
  • 7
0
[HttpPost]
[ActionName("ListarTodosGrupo")]
public ActionResult ListAllFromGroup(string wildcard = "", int registries = 10)
{
    // ...
}

[HttpPost]
[ActionName("ListarTodosGrupo2")]
public ActionResult ListAllFromGroup(string cnpj, string wildcard = "", int registries = 10)
{
    // ...
}

Haven't tried but If you can change your second method's action name to ListarTodosGrupo2 then you could call post method using ajax as below.

$.ajax({
    url: "/Cliente/ListarTodosGrupo",
    type: "post",
    dataType: "json",
    cache: true,
    data: { // This should fire the first action
        wildcard: $("input#nomeCliente").val(),
        registries: 10
    },
    ...
});

$.ajax({
    url: "/Cliente/ListarTodosGrupo2",
    type: "post",
    dataType: "json",
    cache: true,
    data: { // This should fire the second action
        wildcard: $("input#nomeCliente").val(),
        registries: 10,
        cnpj: '02696818000116' 
    },
    ...
});
Dharmesh Parmar
  • 91
  • 1
  • 10