发送一个jQuery数组回一个C#字符串数组 [英] Sending a jQuery array back to a c# String Array
问题描述
我有一个需要的String []
阵列的C#方法,我用 $。的getJSON
来电传递参数的控制方法。
I have a C# method that requires a String[]
array and I use a $.getJSON
call to deliver the parameters to the Method in the Controller.
我正在已选定的复选框的值,并通过他们回到控制器,但是当我使用字符串化
它的地方,如果多个值,在一起,当我通过阵列自身,也有多个条目,但接收的String []
为空。
I am taking the values of Checkboxes that have been selected and passing them back to the Controller, however when I use Stringify
it places if multiple values, together and when I pass the array its self, there are multiple entries, however the receiving String[]
is null.
var array = new Array();
$('#ids:checked').each(function() {
array.push($(this).val())
}
var jsonText = JSON.stringify(array); // I have tried this, but to receive one entry
var dataValues = {
ids: jsonText,
id: $('#idNumber').val(),
orderId: $('#orderId').val()
};
$.getJSON("/path/to/method", dataValues, function(){});
public ActionResult doSomething(String[] ids, Int32 Id, Int32 OrderId)
{
//do something here
}
感谢您的帮助。
推荐答案
您正在设置 IDS
的值JSON字符串;但是,服务器无法知道的方式。至于服务器知道, IDS
是字符串
价值,不能被转换为的String []
。
You are setting the value of ids
to a JSON string; however, the server has no way of knowing that. As far as the server knows, ids
is a string
value, which can't be converted to string[]
.
而不是一个值转换为JSON的,应在整个数据对象转换为JSON并指定其内容类型:
Instead of converting one value to JSON, you should convert the whole data object to JSON and specify its content-type:
var dataValues = {
ids: array, //the Javascript array, *not* the JSON string
id: $('#idNumber').val(),
orderId: $('#orderId').val()
};
$.ajax({
url: "/path/to/method",
data: JSON.stringify(dataValues),
success: function(){},
contentType: "application/json"
});
这篇关于发送一个jQuery数组回一个C#字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!