【问题标题】:ASP MVC Ajax request to ActionResult with string array as parameter以字符串数组为参数的 ASP MVC Ajax 对 ActionResult 的请求
【发布时间】:2014-06-17 15:37:40
【问题描述】:

我有一个必须在今晚完成的小型 Web 应用程序,但我遇到了这个问题。我正在从 Index 视图向我的 HomeController 发出 ajax 请求,该请求传递了一些参数,包括字符串数组。

控制台中字符串数组的输出是:["text1", "text2"] 但在 actionresult 中我得到的是 string[0] = "text1, text1" 而不是 string[0] = "text1 " 和 string[1] = "text2"。

下面是我的代码!

jQuery 填充字符串数组(包裹在 onload 中):

 var keywords = [];

 $("#menu-jesus a").on("click", function () {
        var keyword = $(this).text();
        keywords.push(keyword);

        getFilteredStatues();
        showDescription(keyword);

        $('#statue-info').hide();
    });

阿贾克斯:

$.ajax({
            type: "GET",
            cache: false,
            url: '@Url.Action("GetFilteredStatues")?height=' + queryString[0]
            + "&material=" + queryString[1]
            + "&keywords=" + keywords
            + "&itemsPerPage=" + itemsPerPage,
            dataType: "html",
            success: function (data) {
                $("#pictures").html(data);
                showSearchTerms();
                hideSearchTermDiv();
                getStatueInfo();

                if (itemsPerPage >= 304) {
                    $(".more-results").hide();
                }
            },
        });

操作结果:

    public ActionResult GetFilteredStatues(string height, string material, string[] keywords, int itemsPerPage)
    {
        var h = height != "undefined" ? height : "";
        var m = material != "undefined" ? material : "";

        var statues = db.GetFilteredStatues(h, m, keywords, itemsPerPage);

        return PartialView("PicturesResult", new IndexModel(statues));
    }

感谢任何提示!谢谢。

【问题讨论】:

  • 那么问题是什么?在, 上拆分它们并制作数组等等任何需要的东西

标签: jquery ajax arrays asp.net-mvc asp.net-mvc-4


【解决方案1】:

将您的请求更改为

    var data = {
        height: queryString[0],
        material: queryString[1],
        keywords: JSON.stringify(keywords),
        itemsPerPage: itemsPerPage,
    };

$.ajax({
            type: "GET",
            cache: false,
            url: '@Url.Action("GetFilteredStatues")'
            dataType: "html",
            contentType = 'application/json',
            data: JSON.stringify(data)
            success: function (result) {
                $("#pictures").html(result);
                showSearchTerms();
                hideSearchTermDiv();
                getStatueInfo();

                if (itemsPerPage >= 304) {
                    $(".more-results").hide();
                }
            },
        })

【讨论】:

  • 我在 itemsPerPage 为空时出现错误“参数字典包含方法'System.Web.Mvc'的不可为空类型'System.Int32'的参数'itemsPerPage'的空条目。 ActionResult GetFilteredStatues(System.String, System.String, System.String[], Int32)' in 'Lore.Controllers.HomeController'。可选参数必须是引用类型、可空类型或声明为可选参数。参数名称:parameters" 但是一切似乎都正确传递了。
  • 在发送前检查 itemsPerPage 是否为空
  • 就是这样,不是。真奇怪
  • 使用 fiddler 或 chrome 控制台检查请求
  • 一切似乎都很好。我什至可以看到正确填写了 url 和 itemsPerPage。谢谢你的帮助,我现在真的需要它。
【解决方案2】:

为了让模型绑定器为关键字挑选一个数组,您需要 URL 看起来像:

keywords=string1&keywords=string2

如果您知道关键字中只有两个元素,那么您可以这样做

+ "&keywords=" + keywords[0]
+ "&keywords=" + keywords[1]

如果关键字中有任意数量的元素,那么您需要循环遍历数组并每次添加到字符串中 += "&keywords=" + keywords[i]

【讨论】:

  • 我不知道我可能有多少元素。这听起来像是一个解决方案,但并不是一个真正的好做法。
  • 我不会说这是一个不好的做法,但我不得不承认 Nacho 的解决方案要干净得多。我会走那条路。
  • 赞成这一点,除了是否是不好的做法(为什么会这样?)它是发送数组作为获取参数的标准方式。
猜你喜欢
  • 1970-01-01
  • 2015-04-27
  • 2021-01-11
  • 1970-01-01
  • 1970-01-01
  • 2012-02-16
  • 1970-01-01
  • 2012-02-07
  • 1970-01-01
相关资源
最近更新 更多