[英]Is it possible to convert a MVC C# List<int> to an javascript array?
[英]Convert list to int array in C#
我已经通过StackOverflow和Google搜索了转换,但遗憾的是我无法获得解决方案。 一切都与我想要的相反。 即,将int[]
转换为List
。
问题
我在[WebMethod]
有这个代码。
[WebMethod]
public int MyMethodWS(int N, List<int> M)
{
}
现在我有一个控制台应用程序 ,它使用以下URL引用此服务:
http://localhost:61090/MyMethod.asmx?WSDL
我的控制台应用程序中有此代码:
int N;
List<int> M = new List<int>();
// Some crazy user input coding.
// Ultimately you will have non-empty M list and N int.
MyMethod.MyMethodWS DA = new MyMethod.MyMethodWS();
Sum = DA.MyMethodWS(N, M);
当我运行此代码时,我收到此错误:
#1: '
MyMethod.MyMethod.MyMethodWS(int, int[])
'的最佳重载方法匹配有一些无效的参数。#2:参数2:无法从'
System.Collections.Generic.List<int>
'转换为'int[]
'
问题
List
。 但为什么它试图转换为int[]
? 它试图将其转换为int[]
,因为MyMethodWS
将int[]
作为参数,而不是您尝试传递它的List<int>
。 要将List<int>
转换为int[]
,请调用List<T>.ToArray();
所以改变行Sum = DA.MyMethodWS(N, M);
to Sum = DA.MyMethodWS(N, M.ToArray());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.