基本上,我有两个动作:
[HttpPost]
[ActionName("ListarTodosGrupo")]
public ActionResult ListAllFromGroup(string wildcard = "", int registries = 10)
{
// ...
}
[HttpPost]
[ActionName("ListarTodosGrupo")]
public ActionResult ListAllFromGroup(string cnpj, string wildcard = "", int registries = 10)
{
// ...
}这些操作应该从Ajax调用中调用。我尝试做的是根据Ajax调用参数(data)调用不同的操作。例如:
$.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'
},
...
});然而,它不起作用(尽管给定的参数数量很多,但只触发第一个操作)。这有可能吗?我该怎么做呢?
发布于 2016-05-25 20:13:00
http MVC在调用操作时不考虑重载方法,只考虑方法的名称和任何.NET谓词属性(如HttpPost、HttpGet等)。因此,无论您发送什么数据,都只会调用第一个方法。
你需要重新考虑你的解决方案。最简单的更改是有一个公共方法,ListarTodosGrupos,它接受所有潜在的必要数据,并在适用的情况下将其他数据缺省为null )。您可以为不同的功能创建私有方法。根据发送到ListarTodoGropus的数据,您可以让您的代码调用所需的私有方法。
发布于 2016-05-25 20:28:05
[HttpPost]
[ActionName("ListarTodosGrupo")]
public ActionResult ListAllFromGroup(string wildcard = "", int registries = 10)
{
// ...
}
[HttpPost]
[ActionName("ListarTodosGrupo2")]
public ActionResult ListAllFromGroup(string cnpj, string wildcard = "", int registries = 10)
{
// ...
}我还没有尝试过,但是如果您可以将第二个方法的操作名称更改为ListarTodosGrupo2,那么您可以使用ajax调用post方法,如下所示。
$.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'
},
...
});https://stackoverflow.com/questions/37436132
复制相似问题