【发布时间】:2016-09-22 23:58:01
【问题描述】:
基本上,我有这两个动作:
[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 调用参数(数据)调用不同的操作。例如:
$.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'
},
...
});
但是,它不起作用(尽管给定参数的数量,但仅触发了第一个操作)。甚至可能吗?我该怎么做?
【问题讨论】:
-
删除第一个方法。如果您没有为
cnpj发布值,那么它将只是null -
@StephenMuecke 好点!我不得不承认我想不出这个。但是,这两个功能旨在具有不同的逻辑。第一个(没有'cnpj'参数)应该根据一些会话值找出一个有效的cnpj值,然后调用第二个动作。第二个动作应该从给定(已知)'cnpj'值的任何 ajax 调用中调用。
-
如果为空,执行一项操作,否则执行另一项操作
标签: c# ajax asp.net-mvc-4 action default-parameters