Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 3 在自定义函数中通过ajax调用调用带参数的Action方法_Asp.net Mvc 3_Jquery - Fatal编程技术网

Asp.net mvc 3 在自定义函数中通过ajax调用调用带参数的Action方法

Asp.net mvc 3 在自定义函数中通过ajax调用调用带参数的Action方法,asp.net-mvc-3,jquery,Asp.net Mvc 3,Jquery,我想通过ajax调用调用自定义函数中带有参数的action方法,但这不会调用我的action方法,那么我如何才能用这种方式调用我的action方法呢。 代码如下: <script> function myFunction(e,ids) { var qty = e.value; if (qty == null) { alert("current null : " + qty + " : id - " + ids); } $.ajax({

我想通过ajax调用调用自定义函数中带有参数的action方法,但这不会调用我的action方法,那么我如何才能用这种方式调用我的action方法呢。 代码如下:

<script>
function myFunction(e,ids)
{
    var qty = e.value;
    if (qty == null) {
        alert("current null : " + qty + " : id - " + ids);
    }
     $.ajax({
     type: "POST",
     url:'/MyController/MyActionMethod/Parameter1/Parameter2',
     success: {
          alert("success")
     }

    });

}
</script>

函数myFunction(e,ids)
{
var数量=e.值;
如果(数量=空){
警报(“当前为空:“+qty+”:id-“+ids”);
}
$.ajax({
类型:“POST”,
url:“/MyController/MyActionMethod/Parameter1/Parameter2”,
成功:{
警惕(“成功”)
}
});
}
我的行动方法是:

[HttpPost]
公共操作结果MyActionMethod(int参数1,int参数2){}

请帮帮我。
谢谢。

试试这个,要让
'/MyController/MyActionMethod/Parameter1/Parameter2'
正常工作,您需要设置一个自定义路由

var Parameter1 = '';
var Parameter2 = '';
$.ajax({
    type: "POST",
    url:'/MyController/MyActionMethod?parameter1=' + Parameter1 + '&parameter2=' + Parameter2,
    success: {
        alert("success")
    }
});
另一种方法是包含
数据
参数:

var Parameter1 = '';
var Parameter2 = '';
$.ajax({
    type: "POST",
    url:'/MyController/MyActionMethod',
    data: {
        parameter1: Parameter1,
        parameter2: Parameter2
    },
    success: {
        alert("success")
    }
});

为此,您需要自定义路线

routes.MapRoute(
            "RouteName", // Route name
            "{controller}/{action}/{param1}/{param2}",  // URL with parameters
   new { controller = "Home", action = "Index", param1 = "", param2 = "" } // Parameter defaults
        );
现在尝试使用ajax请求

         $.ajax({
             type: 'POST',
             url:'/MyController/MyActionMethod/Parameter1/Parameter2',
             success: {
                  alert("success")
             }

        });