1

I've been trying to search in both Google and here if there was a question similar to mine but most of the questions involved the opposite of what I want to happen.

So I have an object like this:

class MyObject
{
    int SomeID {get; set;}
    string SomeName {get; set;}
    List<AnotherObjectContainingIDAndString> {get; set;}
}

So after creating my controller and filling up my object, I tried sending it over and when I checked through Postman I get all the properties except for the List one. My question is if this is even possible or if I'm not able to send an array within an object?

Here's how my controller looks like for a bit of context:

[HttpGet("{id}"]
public async Task<ActionResult<MyObj>> GetById(int id)
{
    var myObj = await _context.MyObjs.Where(t => t.Id == id).SingleOrDefaultAsync();
    if (myObj == null)
    {
        return NotFound();
    }
    List<ObjectDescribedInPreviousCodeBlock> myList = new List<ObjectDescribedInPreviousCodeBlock>();
    //Fill the list up
    var toSendOver = new MyObject();
    //Assign the properties to toSendOver
    return toSendOver;
}
6
  • Yes, this is possible. But i am not following your code. You fill up "myObj" at first. But ultimately, you return an empty "toSendOver". Commented Sep 17, 2018 at 19:59
  • @sean I didn't put the logic to keep the snippet short but the comments represent my assigning values into the objects above. So I did a breakpoint to make sure the object is complete before I return the object. I saw the list was filled and inside the object but yeah, when I do a GET request on postman I don't get my array.. Commented Sep 17, 2018 at 20:04
  • 1
    Have you tried adding [DataContract] and [DataMember] to your two classes? I made an answer to show you what i mean. Commented Sep 17, 2018 at 20:14
  • 1
    As one of the answers states, you've oversimplified your code here to the point where it's invalid. On top of that, you've left out what is likely the most important code: the actual creation of the value that's not being serialised. Commented Sep 17, 2018 at 20:20
  • @KirkLarkin I only made it this way cuz I noticed when asking here and pasting lengthy code it ends up with people not bothering to read the questions most of the time. I'll try to give it more context next time. Thanks for the input though. Got my answer, it really was the data contracts haha Commented Sep 17, 2018 at 20:30

3 Answers 3

2

Try decorating your classes to serialize with these:

 [DataContract]
 class MyObject
 {
      [DataMember]
      int SomeID { get; set;}
      [DataMember]
      string SomeName { get; set;}
      [DataMember]
      List<AnotherObjectContainingIDAndString> { get; set;}
 }   
Sign up to request clarification or add additional context in comments.

1 Comment

It works! Thank you so much! I should really make it a habit to read through the docs.. I'm kinda new to using api so I didn't know about the serialization thing.. thanks again.
0

You can normally pass your model to the view but first you must initialize your list variable.

  class MyObject
  {
    public MyObject()
    {
      ListAnotherObjectContainingIDAndString = new List<AnotherObjectContainingIDAndString>();
    }

    int SomeID { get; set; }
    string SomeName { get; set; }
    List<AnotherObjectContainingIDAndString> ListAnotherObjectContainingIDAndString { get; set; }
  }

1 Comment

Yeah I initialized my properties represented by the comments in the code snippet.. my problem is that when I send the object (all of the properties initialized), I don't get the list
0

There's no way this compiles(you must not be showing actual code, so you might have some other simple error we could identify if you posted actual code)

List<AnotherObjectContainingIDAndString> {get; set;}

It's missing a property name such as

List<AnotherObjectContainingIDAndString> ListOfItems {get; set;}

Additionally you'd need to assign the list you created to the object:

toSendOver.ListOfItems = myList;

Lastly you're returning a MyObject which is different type from your action signature which indicates the return is a ViewLessonVM. Although sometimes the framework will map properties, I don't think this will work on returns.

3 Comments

On the last big, I'm sorry I didn't notice I haven't changed the signature. That was what the real code was, I tried to make it less specific cuz it might get more confusing in context and I just tried to generify the classes into names that don't really describe much about the actual objects..
@kobowo good idea to simplify, BUT if I have problems like this, write an action and model that simplifies the problem first and actually test it. This ensures if the problem is still present, the example is accurate, but if the problem goes away in the simplifed problem then you've actually learned something and helped diagnose the problem. Now you start looking at what's different between the two versions. Sometimes the simplified version doesn't exhibit the problem because it doesn't have the same interaction. That's part of the diagnosis you can start with.
Noted, thanks! I'll try that next time too. I honestly made the simplified ones for the post but I didn't think of doing that. Nice idea!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.