Asp.net MVC Ajax 在 GET 上调用空参数 HTML

Asp.net MVC Ajax call null parameter on GET HTML

所以我尝试将整数数组传递给我的控制器。

在我的 ajax 调用中,如果我使用类型:'GET'、数据类型:'HTML',我的整数数组不会传递给控制器​​。 如果我使用 type: 'POST', dataType: 'JSON' 相同的 ajax 调用有效,但是,我需要 return 部分视图。

有什么想法吗?

这是我的代码:

控制器:

    public ActionResult GetLaySimulation(int[] projectIDs)
    {
        var layList = LayHelper.GetLayObjects(projectIDs); //projectIDs = null if I have GET HTML in my ajax call.
        return PartialView("_LaySimulation", layList);
    }

工作 ajax 通话:

    $.ajax({
            url: '@Url.Action("GetLaySimulation", "Admin")',
            type: 'POST',
            data: { projectIDs: simulationIDs },
            dataType: 'JSON',
            success: function (result) {
                hideLoader();
                $("#lay-container").html(result);
            },
            error: function (err) {
                hideLoader();
            }
        });

我需要什么:

   $.ajax({
            url: '@Url.Action("GetLaySimulation", "Admin")',
            type: 'GET',
            data: { projectIDs: simulationIDs },
            dataType: 'HTML',
            success: function (result) {
                hideLoader();
                $("#lay-container").html(result);
            },
            error: function (err) {
                hideLoader();
            }
        });

********************编辑*********************

Javascript 函数:

 $("#lay-container").html("");

    var simulationIDs = [];
    var checkBoxes = $(".chk-export");
    var showButton = false;
    for (var i = 0; i < checkBoxes.length; i++) {
        if ($(checkBoxes[i]).is(":checked") == true) {
            simulationIDs.push($(checkBoxes[i]).attr("data-id"));
        }
    }
    if (simulationIDs.length > 0) {

        $(".btn-excel").fadeIn();
        $("#lay-container").fadeIn();
        showLoader();

        $.ajax({
            url: '@Url.Action("GetLaySimulation", "Admin")',
            type: 'GET',
            data: { projectIDs: simulationIDs },
            dataType: 'HTML',
            success: function (result) {
                hideLoader();
                $("#lay-container").html(result);
            },
            error: function (err) {
                hideLoader();
            }
        });
    }

我找到了这样的解决方案,但我并不满意。 同时,我也不明白为什么我可以传一个字符串参数而不能传一个数组:

   public ActionResult GetLaySimulation(string ids)
    {
        var idsStr = ids.Split(',');
        List<int> projectIDs = new List<int>();
        foreach (var item in idsStr)
        {
            if (!string.IsNullOrEmpty(item))
                projectIDs.Add(int.Parse(item));
        }
        var layList = LayHelper.GetLayObjects(projectIDs.ToArray());
        return PartialView("_LaySimulation", layList);
    }

查看:

  function chkClicked() {
    $("#lay-container").html("");

    var simulationIDs = "";
    var checkBoxes = $(".chk-export");
    var showButton = false;
    for (var i = 0; i < checkBoxes.length; i++) {
        if ($(checkBoxes[i]).is(":checked") == true) {
            simulationIDs += $(checkBoxes[i]).attr("data-id") + ",";
        }
    }
    if (simulationIDs.length > 0) {

        $(".btn-excel").fadeIn();
        $("#lay-container").fadeIn();
        showLoader();

        $.ajax({
            url: '@Url.Action("GetLaySimulation", "Admin")',
            type: 'GET',
            data: { ids: simulationIDs },
            dataType: 'HTML',
            success: function (result) {
                hideLoader();
                $("#lay-container").html(result);
            },
            error: function (err) {
                hideLoader();
            }
        });
    }

    else {
        $(".btn-excel").fadeOut();
        $("#lay-container").fadeOut();
    }
}

您可以在查询字符串中发送数组。 Javascript 代码:

    $.ajax({
        url: '@Url.Action("GetLaySimulation", "Admin")' + "?projectIDs=" + JSON.stringify(simulationIDs),
        type: 'GET',
        dataType: 'HTML',
        success: function (result) {
            hideLoader();
            $("#lay-container").html(result);
        },
        error: function (err) {
            hideLoader();
        }
    });

要检索控制器:

public ActionResult GetLaySimulation()
{
    var ids = HttpContext.Request.QueryString["projectIDs"];
    int[] projectIDs = JsonConvert.DeserializeObject<int[]>(ids);
    // Code....
}    

你可以使用 POST 类型和 html 数据类型来完成它 不要混淆 ☺