无法将数据/参数传递给ajax调用 [英] Can not pass data/parameter to an ajax call

查看:75
本文介绍了无法将数据/参数传递给ajax调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有下拉菜单和部分视图的简单网页.我正在尝试根据下拉选定值更改时启动的ajax调用的结果来更新部分视图.我可以执行控制器操作,但是我传递的参数始终为null.

I have a simple web page with a dropdown and a partial view. I am trying to update the partial view based on the result of the ajax call initiated when the dropdown selectedvalue changes. I can reach the controller action, but the parameter I am passing is always null.

我尝试了以下几种方式来传递数据.

I have tried the following ways to pass the data.

    var formData = new FormData();
    formData.append('Id', managerId)

    data:formData,
    data: { 'Id': managerId } ,
    data: JSON.stringify({ 'Id': managerId } ),

这是控制器动作.我使用默认的MVC路由,没有定义其他路由.

Here is the Controller Action. I use the default MVC routing, there are no other routing defined.

    [HttpPost]
    public ActionResult GetEmployees(string Id)
    {
        if (string.IsNullOrEmpty(Id))
        {
            Id = "2";
        }
        long numberId = Convert.ToInt64(Id);
        List<SEmployee> lstEmp = db.SEmployees.Where(x => x.LkupParentManagerId == numberId).ToList();
        return PartialView("_EmployeeTable", lstEmp);
    }

这是完整的Ajax调用:

Here is the complete Ajax call:

$(document).ready(function () {
$("#ddManager").change(function () {
    var managerId = this.value;
    var formData = new FormData();
    var form = $('form').serialize();
    formData.append('Id', managerId);
    //alert(managerId);
    $.ajax({
        url: "/SEmployees/GetEmployees",
        type: 'POST',
        data:formData,
        //data: { 'Id': managerId } ,
        //data: JSON.stringify({ 'Id': managerId } ),
        processData: false,
        success: function (result) {
            //alert(result);
            $("#tblEmployees").html(result);
        },
        error: function () {
            alert('Failed to retrieve the Employees.');
        }
    });
});

});

这是视图中的下拉列表.

Here is the dropdown in the view.

    <div class="form-group">
            @Html.LabelFor(model => model.SelectedId, htmlAttributes: new { @class = "control-label required" })
            <div>
                @Html.DropDownListFor(model => model.SelectedId, Model.LstSelectManagers, new { @id = "ddManager", @class = "form-control", @margin = "auto" })
                @Html.ValidationMessageFor(model => model.SelectedId, "", new { @class = "text-danger", AutoPostBack = "True" })
            </div>
        </div>               

我尝试了以上三种不同的方式将数据从视图传递到控制器.呼叫转到控制器,但不转到数据.请帮助我对ajax调用进行故障排除.预先感谢.

I have tried the above three different ways to pass the data from view to controller. The call goes to the controller, but not the data. Please help me troubleshooting the ajax call. Thanks in advance.

推荐答案

将此用于您的ajax

Use this for your ajax

$.ajax({
    url: "/SEmployees/GetEmployees/" + managerId,
    type: 'POST',
    processData: false,
    success: function (result) {
        //alert(result);
        $("#tblEmployees").html(result);
    },
    error: function () {
        alert('Failed to retrieve the Employees.');
    }
});

这篇关于无法将数据/参数传递给ajax调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆