通过数组从JavaScript到控制器MVC 4 [英] passing array from javascript to controller MVC 4

查看:114
本文介绍了通过数组从JavaScript到控制器MVC 4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这篇关于通过数组从JavaScript到控制器MVC 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆