Reputation: 2787
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;
}
Upvotes: 1
Views: 784
Reputation: 1205
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;}
}
Upvotes: 2
Reputation: 38394
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.
Upvotes: 0
Reputation: 152
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; }
}
Upvotes: 0