繁体   English   中英

将数组从javascript传递给控制器​​MVC 4

[英]passing array from javascript to controller MVC 4

我正在使用剃刀,我很难将数组传递给控制器​​。 数组包含我制作的对象,我正在尝试这样做:

$.ajax({
     type: "POST",
     url: "HomePage/HandleOperations",
     data: JSON.stringify(operationCollection),
     success: function (data) { alert("SUCESS");},
     dataType: "json",
     contentType: "application/json"
});

我的控制器是:

 public void HandleOperations(List<string> operationCollection)
 {

 }

我不需要使用ajax,但我不确定如何才能完成。 在控制器中,它显示“operationCollection”包含元素,但它们都是null。

Ajax参数

traditional : true

会做的。

客户端:

$.ajax({
     type: "POST",
     url: "HomePage/HandleOperations",
     data: {operations: operationCollection},
     success: function (data) { alert("SUCCESS"); }
});

并声明类服务器端如下:

public class Operation
{
  public int Index;
  public string Source;
  public string Target;
  public int AddOrDel;
}

然后你可以采取这个行动:

public void HandleOperations(Operation[] operations)
{
}

使用传统:ajax调用的true参数:

为了帮助radbyx,使用ajax调用的“traditional:true”属性,如下所示,将告诉ajax使用传统的序列化形式。 更多细节: http//api.jquery.com/jQuery.param/或者什么是JQuery中的“传统样式的param序列化”

$.ajax({
     type: "POST",
     url: "HomePage/HandleOperations",
     data: {operations: operationCollection},
     traditional: true,
     success: function (data) { alert("SUCCESS"); }
});

在ajax中添加“traditional:true”参数。

例:

var parentValueToPush=new Array();
$('[name=SelectedUsers]:checked').each(function () {
            parentValueToPush.push($(this).val());
            temp.push($(this).val());
        });
        $.ajax({
            url: //URL,
            type: 'get',
            traditional: true,
            dataType: 'html',
            cache: false,
            data: { SelectedUsers: parentValueToPush},
            success: function (data) {
                   //Result
            }
        });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM