开发者

Receiving AJAX jQuery array in c#

开发者 https://www.devze.com 2023-03-23 03:45 出处:网络
I have a JSON array like this: \"{structChangeList : \'[{\"structChange\":{\"id\":\"Level1.Second\",\"parentId\":\"Level1.First\",\"date\":\"2011-01-01\"}}]\'\"

I have a JSON array like this:

  "{structChangeList : '[{"structChange":{"id":"Level1.Second","parentId":"Level1.First","date":"2011-01-01"}}]'"

or

"[{"structChangeList":{"id":"Level1.Second","parentId":"Level1.First","date":"2011-01-01"}}]"

and various other variationd which I am trying to pick up with a web method like

    [WebMethod]
    public string receiveStructureUpdates3(List<StrutureData> structChangeList)
    {

        return "Hello World";
    }

where StructureData is:

[DataContract]
public class StrutureData
{
    [DataMember]
    public string id { get; set; }
    [DataMember]
    public string parentId { get; set; }
    [DataMember]
    public string date { get; set; }
}

It works fine when I try to pick up a non array like:

"{"structChange":{"id":"Level1.Secon开发者_如何学JAVAd","parentId":"Level1.First","date":"2011-01-01"}}"

with:

    [WebMethod]
    public string receiveStructureUpdates2(StrutureData structChange)
    {
    }

But I can't get the array working. Any ideas?


EDIT:

To use an array/list, change:

"{"structChange":{"id":"Level1.Second","parentId":"Level1.First","date":"2011-01-01"}}"

To

{"structChange": [{"id":"Level1.Second","parentId":"Level1.First","date":"2011-01-01"}]}

Because .Net is looking for structChage, it wants to find that first. Since structChange is a List the value for that key needs to be an array.


This is the correct way to send data to an action in the controller

data: "{someField : [[\"Level1.Second\",\"Level1.First\",\"2011-01-01\"]] }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号