从Javascript传递到C#中的数组 [英] Pass a array from javascript to c#

查看:194
本文介绍了从Javascript传递到C#中的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在javascript中的数组,我需要得到它我的C#的WebMethod。什么是做到这一点的最好方法是什么?

我的C#code是这样的:

  [的WebMethod]
公共静态无效SaveView(字符串[] myArray的,字符串[]过滤器)
{
}

编辑 -

我的JSON数据是这样的:

<$p$p><$c$c>{\"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阵列= ['富','酒吧','巴兹'];
$阿贾克斯({
    网址:'/foo.aspx/SaveView',
    输入:POST,
    的contentType:应用/ JSON,
    数据:JSON.stringify({myArray的:数组})
    成功:函数(结果){    }
});

如果您页方法返回的东西,你应该使用 result.d 属性成功回调抓取网页方法调用的结果。

如果你不使用jQuery,你必须手动占发送AJAX请求浏览器的差异。但是对于这个工作有被包含在请求2关键事


  • 的Content-Type的请求头必须设置为应用程序/ JSON

  • 请求负载应该是JSON,例如: {myArray的:'富','酒吧','巴兹']}


更新:

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

 公共类模型
{
    公共字符串名称{;组; }
    公共字符串指数{搞定;组; }
    公共BOOL隐藏{搞定;组; }
    公众诠释标识{搞定;组; }
    公共BOOL可排序{搞定;组; }
    公共SearchOption Searchoptions {搞定;组; }
    公众诠释宽度{获得;组; }
    公共BOOL标题{搞定;组; }
    公众诠释WidthOrg {搞定;组; }
    公共BOOL可调整大小{搞定;组; }
    公共字符串标签{搞定;组; }
    公共布尔搜索{搞定;组; }
    公共字符串S型{搞定;组; }
}公共类SearchOption
{
    公共字符串[] {SOPT获得;组; }
}

和则:

  [的WebMethod]
公共静态无效SaveView(型号[] myArray的)
{
}

I have a array in javascript and i need to get it to my c# webMethod. what is the best way to do this?

my c# code is like this:

 [WebMethod]
public static void SaveView(string[]  myArray, string[] filter)
{
}

EDIT--

My json data looks like this:

{"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"}]}

But i doesnt work... any idea why?

Thank you very much.

解决方案

You could send it as a JSON string. Here's an example using jQuery:

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

    }
});

If your Page Method returns something, you should use the result.d property in the success callback to fetch the result of the page method call.

If you don't use jQuery, you will have to manually account for browser differences in sending the AJAX request. But for this to work there are 2 crucial things to be included in the request:

  • The Content-Type request header must be set to application/json
  • The request payload should be JSON, for example: { myArray: [ 'foo', 'bar', 'baz' ] }

UPDATE:

Now that you have updated your question it seems that you are no longer willing to send an array of strings. So define a model that will match the JSON structure you are sending:

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; }
}

and then:

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

这篇关于从Javascript传递到C#中的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆