将数组从javascript传递到c#

将数组从javascript传递到c#,c#,javascript,arrays,string,C#,Javascript,Arrays,String,我在javascript中有一个数组,我需要将它放到我的c#webMethod中。最好的方法是什么 我的c代码是这样的: [WebMethod] public static void SaveView(string[] myArray, string[] filter) { } {"myArray":[{"name":"Title","index":"Title","hidden":false,"id":"1","sortable":true,"searchoptions":{"sopt"

我在javascript中有一个数组,我需要将它放到我的c#webMethod中。最好的方法是什么

我的c代码是这样的:

 [WebMethod]
public static void SaveView(string[]  myArray, string[] filter)
{
}
{"myArray":[{"name":"Title","index":"Title","hidden":false,"id":"1","sortable":true,"searchoptions":{"sopt":["cn","eq","bw","ew"]},"width":419,"title":true,"widthOrg":150,"resizable":true,"label":"Title","search":true,"stype":"text"},{"name":"Author","index":"Author","hidden":false,"id":"3","sortable":true,"searchoptions":{"sopt":["cn","eq","bw","ew"]},"width":419,"title":true,"widthOrg":150,"resizable":true,"label":"Author","search":true,"stype":"text"}]}
编辑--

我的json数据如下所示:

 [WebMethod]
public static void SaveView(string[]  myArray, string[] filter)
{
}
{"myArray":[{"name":"Title","index":"Title","hidden":false,"id":"1","sortable":true,"searchoptions":{"sopt":["cn","eq","bw","ew"]},"width":419,"title":true,"widthOrg":150,"resizable":true,"label":"Title","search":true,"stype":"text"},{"name":"Author","index":"Author","hidden":false,"id":"3","sortable":true,"searchoptions":{"sopt":["cn","eq","bw","ew"]},"width":419,"title":true,"widthOrg":150,"resizable":true,"label":"Author","search":true,"stype":"text"}]}
但是我不工作。。。知道为什么吗


非常感谢。

您可以将其作为JSON字符串发送。下面是一个使用jQuery的示例:

var array = [ 'foo', 'bar', 'baz' ];
$.ajax({
    url: '/foo.aspx/SaveView',
    type: 'POST',
    contentType: 'application/json',
    data: JSON.stringify({ myArray: array }),
    success: function(result) {

    }
});
如果Page方法返回某些内容,则应使用成功回调中的
result.d
属性来获取Page方法调用的结果

如果不使用jQuery,则必须手动考虑发送AJAX请求时浏览器的差异。但要使其发挥作用,请求中需要包括两个关键事项:

  • 内容类型请求标头必须设置为
    application/json
  • 请求负载应该是JSON,例如:
    {myArray:['foo','bar','baz']}

更新:

现在您已经更新了问题,似乎不再愿意发送字符串数组。因此,请定义一个与您发送的JSON结构匹配的模型:

public class Model
{
    public string Name { get; set; }
    public string Index { get; set; }
    public bool Hidden { get; set; }
    public int Id { get; set; }
    public bool Sortable { get; set; }
    public SearchOption Searchoptions { get; set; }
    public int Width { get; set; }
    public bool Title { get; set; }
    public int WidthOrg { get; set; }
    public bool Resizable { get; set; }
    public string Label { get; set; }
    public bool Search { get; set; }
    public string Stype { get; set; }
}

public class SearchOption
{
    public string[] Sopt { get; set; }
}
然后:

[WebMethod]
public static void SaveView(Model[] myArray)
{
}

在asp.NETCore2.2中,我尝试将一组对象从js发送到控制器

JS Ajax函数-

    url: '/UserManagement/Calender/CalendarPreview/',
    data: { "list": AllSchedulesarr},
    CalendarPreview(List<CreateOrUpdateBookingSlotDtos> list)
控制器-

    url: '/UserManagement/Calender/CalendarPreview/',
    data: { "list": AllSchedulesarr},
    CalendarPreview(List<CreateOrUpdateBookingSlotDtos> list)
日历预览(列表)
不行,这样不行
myArray=…
是无效的JSON。你必须把整个文字串起来。请参阅我的答案以获取示例。否,在这种情况下,您需要JSON,否则OP尝试调用的ASP.NET PageMethod将无法工作。ASP.NET PageMethods需要JSON编码的请求并发送JSON编码的结果。@DarinDimitrov PageMethods不能与“application/x-www-form-urlencoded”一起使用?谢谢,我的webMethod应该是什么样子?@Ovi,你希望我怎么知道这一点?这将取决于您希望此方法执行的操作。但是您显示的签名是正确的,它将收到
string[]
参数:
[WebMethod]public static void SaveView(string[]myArray){…}
。您的web方法似乎很好-您必须按照您的要求处理数组。这完全取决于您,我们无法真正帮助您。:)我编辑了我的问题,你能看一下吗?我还输入了正确的方法签名。。。谢谢大家!@奥维,你完全改变了你的问题。你从哪里得到JSON的?您怎么可能期望此结构与page方法所期望的
string[]
参数有关。如果你想接收这样一个结构的数组,你必须定义并调整你的签名。让我更新答案。-1您更新的问题与原始问题完全不同。如何将该对象数组转换为字符串数组
{“name”:“Title”,“index”:“Title”,“hidden”:false,“id”:“1”,“sortable”:true,“searchoptions”:{“sopt”:[“cn”,“eq”,“bw”,“ew”]},