Firstly, thanks in advance for anyone who can help.
I have a header and lines scenario. I want the lines object to auto-populate with the headerId from the Header. Please can someone help?
public class Header
{
public int headerId { get; set; }
public List<Lines> lines { get; set; }
}
public class Lines
{
public int lineId { get; set; }
public int headerId { get; set; } //<<< want to autopopulate this from Header object
}
There's a number of ways you can do this.
Option 1 - Fix on collection set.
The problem with this route is that the HeaderId is only set when you set
Lines
collection. That means you can change it after the fact or any new items you add toLines
will not be corrected.Option 2 - Pass in the Header when constructing the Line
As long as you have the same
Header
instance for all of the lines, if you were change theHeader.HeaderId
all of the lines will automatically be updated.Option 3 - Don't add an instance of Line directly to the collection
This will have the same issue as Option 1.