通过AJAX向控制器动作传递数组

5

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

    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,使用traditional:true,不使用traditional:true。
在服务器端,我尝试了object[]、object、List、List、IEnumerable等参数类型。
但是都没有起作用!应该如何正确地处理它? 解决方案: 我的问题很简单——数组中的实际值之一带有HTML标记。只需要在动作方法中添加[ValidateInput(false)]即可。

你有收到任何错误信息吗?如果在控制器方法中设置断点,它是否被触发? - Dr Rob Lang
4个回答

4

你只需要以下设置:

通过Ajax将字符串数组传递给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:

我还使用以下代码将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
       }
});

帮了我,谢谢!顺便说一句,不需要 traditional: true。 - Pierre Anken
2
这应该是被接受的答案。非常感谢。 - Nhân Nguyễn

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

试试这个...


1
至少,您可以将JavaScript数组作为字符串传递,并在控制器中对其进行反序列化。
public JsonResult JsonGetStatesInfo(string instructions)

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

或者按照这里的解释使用新数组:https://dev59.com/dXVC5IYBdhLWcg3wZwJT#310136


0

尝试添加:

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

请你删除 AJAX 调用中的以下内容:
JSON.stringify(instructions);

因为您正在尝试将一个对象转换为字符串,这将被发送到服务器作为字符串而不是对象(而服务器端期望一个对象)。
您也可以删除这个。
traditional: true,
ache: false,

我遇到了客户端错误:"无效的 JSON 原始指令" - Oleg Sh
尝试使用上述建议中的JSON.stringify。 - Koby Douek

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接