首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >具有相同名称和不同[默认]参数的POST操作不会从Ajax调用中触发

具有相同名称和不同[默认]参数的POST操作不会从Ajax调用中触发
EN

Stack Overflow用户
提问于 2016-05-25 19:44:12
回答 2查看 87关注 0票数 0

基本上,我有两个动作:

代码语言:javascript
复制
[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)调用不同的操作。例如:

代码语言:javascript
复制
$.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' 
    },
    ...
});

然而,它不起作用(尽管给定的参数数量很多,但只触发第一个操作)。这有可能吗?我该怎么做呢?

EN

回答 2

Stack Overflow用户

发布于 2016-05-25 20:13:00

http MVC在调用操作时不考虑重载方法,只考虑方法的名称和任何.NET谓词属性(如HttpPost、HttpGet等)。因此,无论您发送什么数据,都只会调用第一个方法。

你需要重新考虑你的解决方案。最简单的更改是有一个公共方法,ListarTodosGrupos,它接受所有潜在的必要数据,并在适用的情况下将其他数据缺省为null )。您可以为不同的功能创建私有方法。根据发送到ListarTodoGropus的数据,您可以让您的代码调用所需的私有方法。

票数 0
EN

Stack Overflow用户

发布于 2016-05-25 20:28:05

代码语言:javascript
复制
[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方法,如下所示。

代码语言:javascript
复制
$.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' 
    },
    ...
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37436132

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档