【问题标题】:Asp.net MVC Ajax call null parameter on GET HTMLAsp.net MVC Ajax 在 GET HTML 上调用空参数
【发布时间】:2019-10-01 14:11:47
【问题描述】:

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

在我的 ajax 调用中,如果我使用 type: 'GET', dataType: 'HTML',我的整数数组不会传递给控制器​​。 如果我使用 type: 'POST', dataType: 'JSON' ,同样的 ajax 调用可以工作,但是我需要返回一个局部视图。

有什么想法吗?

这是我的代码:

控制器:

    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();
            }
        });
    }

【问题讨论】:

  • 是什么阻止您使用从发布请求返回的值作为部分视图?
  • 它不返回局部视图。它返回一个 Json 结果。
  • simulationIDs a JavaScript 数组吗?
  • 是的,我编辑了问题以显示完整的功能。

标签: c# ajax asp.net-mvc-5


【解决方案1】:

你可以使用 type 作为 POST 和 dataType 作为 html 不要混淆 ☺

【讨论】:

  • 我要试试这个。
【解决方案2】:

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

    $.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....
}    

【讨论】:

    【解决方案3】:

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

       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();
        }
    }
    

    【讨论】:

    • 你在获取请求中传递数组。如果你想传递多个值,你可以通过将它们作为逗号分隔的字符串或在 post 请求中传递一个 json 正文有效负载来实现。为什么你不想使用帖子?
    • 我需要返回部分视图。我不知道如何通过帖子返回部分视图。
    • 当您使用 POST 操作执行 return PartialView("_LaySimulation", layList); 时会发生什么?
    • 我要试试这个。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-27
    • 2019-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-20
    • 1970-01-01
    相关资源
    最近更新 更多