【问题标题】:Pass array to Controller Action via AJAX通过 AJAX 将数组传递给控制器​​动作
【发布时间】:2017-02-16 10:24:36
【问题描述】:

我看到了类似的问题,但没有一个对我有帮助。 我有最简单的代码:

    public JsonResult JsonGetStatesInfo(object[] instructions)
    {
        if (Request.IsAjaxRequest())
        {

            return Json(String.Empty);
        }
        else
            throw new NoAjaxRequestException();
    }

和客户端:

            var instructions = [];
            instructions.push('abc');
            instructions.push('ddd');
            instructions.push('assdbc');
            var inst = JSON.stringify(instructions);
            $.ajax({
                cache: false,
                data: { 'instructions': inst },
                traditional: true,
                dataType: 'json',
                url: '/State/JsonGetStatesInfo',
                type: 'post',
                success: function (resp) {
                },
                error: function (data) {
                    alert(data.error);
                }
            });

在客户端,我尝试使用 JSON.stringify,不使用 JSON.stringify,使用传统:true,不使用传统:true

在服务器端,我尝试作为参数:object[]、object、List、List、IEnumerable

没有任何效果!如何正确操作?

已解决: 我的问题是微不足道的 - 来自数组的真实值的一个有 HTML 标签。只需将 [ValidateInput(false)] 添加到操作方法

【问题讨论】:

  • 您是否收到任何错误消息?如果在控制器方法中设置断点,是否命中?

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


【解决方案1】:

您只需要以下设置:

通过 Ajax 将 string 数组传递给 MVC 操作:

控制器:

public JsonResult MyAction(string[] instructions)
{
    // Your codes
}

查看:

$.ajax({
       data: { 'instructions': ['abc', 'dcs', 'arr'] }, // << Without JSON.stringify 
       traditional: true,  // << Important line
       url: '/State/MyAction',
       type: 'post',
       dataType: 'json',
       success: function (resp) {
            // Your codes
       }
});

也推荐使用contentType: "application/json; charset=utf-8"


通过 Ajax 将 int 数组传递给 MVC Action:

我还使用下面的代码将array of int 指向Action

控制器:

public JsonResult MyAction(int[] instructions)
{
    // Your codes
}

查看:

$.ajax({
       data: { 'instructions': [1, 2, 3] }, // << Without JSON.stringify 
       traditional: true,  // << Important line
       url: '/State/MyAction',
       type: 'get',
       dataType: 'json',
       success: function (resp) {
            // Your codes
       }
});

【讨论】:

  • 帮了我谢谢!顺便说一句,不需要传统的:真的,
  • 这应该是公认的答案。非常感谢
【解决方案2】:
public ActionResult JsonGetStatesInfo(string[] instructions)
{
   return new JsonResult
        {
            JsonRequestBehavior = JsonRequestBehavior.AllowGet,
            Data = new { result = instructions.Length}
        };
}

客户端

 var instructions = ['abc', 'dcs', 'arr'];
 $.post('/State/JsonGetStatesInfo', { instructions: instructions },
      function (data) {
       if (data.result!= null && data.result!= undefined) {
        alert(data.result);
    }
});

试试这个……

【讨论】:

    【解决方案3】:

    至少,您可以将javascript数组作为字符串传递并在控制器中对其进行反序列化

    public JsonResult JsonGetStatesInfo(string instructions)
    
    var instructionsArray= JsonConvert.DeserializeObject<string[]>(instructions);
    

    或者使用这里解释的新数组:https://stackoverflow.com/a/310136/3063094

    【讨论】:

      【解决方案4】:

      尝试添加:

      contentType: "application/json; charset=utf-8"
      

      给你AJAX调用,请删除:

      JSON.stringify(instructions);
      

      因为您正在尝试对对象进行字符串化,该对象将作为字符串而不是对象发送到服务器(并且服务器端需要一个对象)。

      你也可以删除

      traditional: true,
      ache: false,
      

      【讨论】:

      • 我收到客户端错误:“无效的 JSON 原始指令”
      • 按照上面的建议尝试使用 JSON.stringify。
      猜你喜欢
      • 1970-01-01
      • 2011-12-21
      • 2016-09-20
      • 2019-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-05
      • 2017-04-18
      相关资源
      最近更新 更多