通过数组从JavaScript到控制器MVC 4数组、控制器、JavaScript、MVC

2023-09-10 14:34:57 作者:六个字的昵称:

我使用的剃刀,我有一个很难将数组传递给控制器​​。数组包含我做了对象,我试图做到这一点:

I am using razor and I'm having a hard time passing an array to a controller. the array contains objects that I made and I am trying to do this:

$.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包含的元素,但它们均为空。

I am not required to use ajax but I am not sure how else it could be done. In the controller it shows that the "operationCollection" contains elements but they are all null.

推荐答案

客户端:

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

和声明一个类服务器端是这样的:

and declare a class server side like this:

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

那么你可以有这样的动作:

then you can have this action:

public void HandleOperations(Operation[] operations)
{
}