简体   繁体   中英

Pass collection of objects inside an object from js to C#

This question can be very easy for you but I cannot find the solution anywhere, I've searched on SO as well. How can I send a collection of objects inside an object to a C# API method, here is my code that I try to send

 {
  "Question":"sdvsdv",
  "PollQuestions":
            [
              {
                "QuestionText":"Question Text"
              }
            ]
}

I've tried with Advanced REST client as well (not only from JS), I can see value of parent object's parameters but PollQuestions objects parameters are all empty.

在此处输入图片说明

Please suggest a work around. Its been couple of days and I cannot get through it. Thanks in advance.

EDIT:

Here is how API method looks like: 在此处输入图片说明

Advanced Rest client request: 在此处输入图片说明

JS request: 在此处输入图片说明

Poll is a simple class like this:

[Serializable]
[DataContract(Namespace = "")]
public class Poll
{
    [DataMember]
    public string Question
    {
            get;
            set;
    }

    [DataMember]
    public Collection<PollQuestion> PollQuestions { get; set; }

}

PollQuestion object:

[Serializable]
public class PollQuestion : AnotherClass
{
    public string QuestionText
    {
            get;
            set;
    }
}

In your case if the problem is to Map the request JSON in C# then try below code:

Class structure:

public class PollQuestion
{
    public string QuestionText { get; set; }
}

public class RootObject
{
    public string Question { get; set; }
    public List<PollQuestion> PollQuestions { get; set; }
}

In Post use [FromBody] attribute to map JSON to C# class:

public void YourMethod([FromBody] RootObject data)
{
 // your code to use request data
}

If you are using WebAPI version="5.2.3" (Or might be supported in the lower version also) no need to serialize requested JSON to C# class. Use this to generate C# classes for JSON .

For JSON structure C# treats,

this:

{

}

as Object and

this:

[]

to List<> OR Array .

I set up a simple sample project in .NET Core and it seems to be working fine for me with your JSON data.

These are the classes I used:

public class PollQuestion
{
    public string QuestionText { get; set; }
}

public class Poll
{
    public string Question { get; set; }
    public List<PollQuestion> PollQuestions { get; set; }
}

And here is the API call function:

    [HttpPost]
    public void Post([FromBody]Poll value)
    {

    }

When I make a postman request the Poll class is filled as expected.

邮差

控制者

Here is also a link to my sample project: https://bitbucket.org/alleskapaul/ld42/downloads/JsonExample.zip

Could you change your models in your project to match mine and see if that works? My guess would be that your model is incorrect as the JSON request works for me as well. If it is not working could you upload a sample project so I can see if I can modify it to work?

I was inheriting a class that was using [DataContract] and [DataMember] attributes, so I had to use DataMember for all parameters of the child class as well.

Thanks everyone for your answers but [FromBody] is not required in an API method when parameter is of complex type.

It works without DataContract and DataMember as well if you have not used DataMember anywhere in the class and also not inheriting any class that uses it.

You can get more info here: When to use DataContract and DataMember attributes?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM