JS int数组到MVC3控制器数组、控制器、JS、int

2023-09-10 19:08:49 作者:微痞少年

我有在JS字符串数组。所有成员实际上是数字。 我的控制器有一个int []参数。

I've got an array of strings in JS. All the members are actually numbers. My controller has an int[] parameter.

我从jQuery的发送:

I send it from jquery:

$.ajax({url: someUrl, data: {ids : JSON.stringify(id_array) }, ...)

和我接受它使用

public ActionResult MyAction(int[] ids) { ...

参数没有得到填补,我检查时,的Request.Form [IDS] 包含 [\25 \,\26 \],该字符串重新JSON数组的presention。 有没有什么办法可以自动执行此操作的不很多INT解析?

The parameter doesn't get filled, I've checked, the Request.Form["ids"] contains "[\"25\",\"26\"]", the string represention of the JSON array. Is there any way to do this automatically without a lot of int parsing?

推荐答案

您是否尝试过不字符串化数组,并让MVC的默认模型绑定做魔术

Have you tried not stringifying the array and letting mvc's default model binding do the magic

$.ajax({url: someUrl, data: {ids : id_array }, ...)

我是pretty的肯定.NET MVC会看到阵列,看看它是int数组并将其映射到你的整数数组正确

I'm pretty sure .net MVC will see the array and see it is an array of ints and map it to your array of ints correctly