简体   繁体   English

MVC Ajax 调用 Controller 方法与参数连接但参数值为 null

[英]MVC Ajax call to Controller Method with Parameter connects but Parameter value is null

I am able to connect my ajax call to the controller, but the parameter is showing as null when it reaches the controller.我能够将我的 ajax 调用连接到 controller,但是当它到达 Z594C103C103F2F0C1AZ 时,参数显示为 null。 What is the correct way for me to pass parameters with this call?我通过此调用传递参数的正确方法是什么? ''' '''

  //In the Index Controller
  [HttpGet]
    public IActionResult TestFunction(string par1)
    {
        int i = 2;
        return view()
    } 

    function onDropDownChange() {
    alert('We are starting to call function');

    var url = '/Index/TestFunction/Cat'
    $.ajax({
        url: url,
        data: {},
        type: 'GET',
        datatype: 'json',
        success: function (data) { alert('Successful call') },
        error: function () {alert('Failed Call')}
        })
}  

   '''

Use data option of ajax.使用 ajax 的data选项。 You can send data object to server by data option in ajax.您可以通过 ajax 中的数据选项将数据 object 发送到服务器。

Example--例子 -

 function onDropDownChange() {

    var url = '/Index/TestFunction/Cat'
    $.ajax({
        url: url,
        data: {par1: "Some value"},//pass your obect
        type: 'GET',
        datatype: 'json',
        success: function (data) { alert('Successful call') },
        error: function () {alert('Failed Call')}
        })
}  

Please check this bind drop-down value in controller action请在 controller 操作中检查此绑定下拉值

function onDropDownChange(elem) {
    alert('We are starting to call function');

    var url = '/Index/TestFunction/Cat'
    $.ajax({
        url: url,
        data: {par1:$(elem).val()},
        type: 'GET',
        datatype: 'json',
        success: function (data) { alert('Successful call') },
        error: function () {alert('Failed Call')}
        })
}  

In view need to pass this argument while calling this function.鉴于调用此 function 时需要传递此参数。

Like eg像例如

<select onchange="onDropDownChange(this)">
///<option> tag
</select>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM