如何使用javascript在mvc控制器中传递数组

时间:2018-08-25 11:42:45

标签: asp.net-mvc

//I convert canvas to Imagesource bytes
byte[] canvasdata = CanvasWriter.SaveCanvasToImgSimulate(canvas, (int)canvas.Width, (int)canvas.Height);

var writer = new AviWriter("test.avi")
{
    FramesPerSecond = 30,
    // Emitting AVI v1 index in addition to OpenDML index (AVI v2)
    // improves compatibility with some software, including 
    // standard Windows programs like Media Player and File Explorer
    EmitIndex1 = true
};

var stream  = writer.AddMotionJpegVideoStream((int)data.CanvasW, (int)data.CanvasH, 70);

stream.WriteFrame(true, canvasdata, 0, canvasdata.Length);

writer.Close();

1 个答案:

答案 0 :(得分:3)

在代码中,您应该首先创建数组,然后发布它。似乎您在循环的每一步都发送数据。那就是问题所在。

在此链接中,您可以看到将数组发送到mvc控制器的示例: How can I post an array of string to ASP.NET MVC Controller without a form?

var __POST = [];
__POST.push({ Id: 1, Images: "aaa", Message: "aaaa" })
__POST.push({ Id: 2, Images: "bbb", Message: "bbbb" })
__POST.push({ Id: 3, Images: "ccc", Message: "cccc" })
__POST.push({ Id: 4, Images: "ddd", Message: "dddd" })


var Post = new Array();
for (var i = 0; i < __POST.length; i++) {
    Post.push({ Id: __POST[i].Id, Images: __POST[i].Images, Message: __POST[i].Message });
}
$.ajax({
    type: "post",
    url: '/Home/Contact',
    data: { model: Post },
    success: function (responses) {

    }
});

在您的Controller中,您需要创建如下类:

public class MYCLASS
{
    public string Id { get; set; }
    public string Images { get; set; }
    public string Message { get; set; }
}

并获取这样的数据:

[HttpPost]
public ActionResult Contact(List<MYCLASS> model)
{
    return View();
}

在该示例中您可以看到,我创建了一个数组并将其推入另一个数组并将其发送到MVC Controller。