Add to Existing Model based on a POCO with need to

2020-04-18 06:49发布

I have a poco model that looks like this:

public int Id {get; set;
public string EmailAddress { get; set; }
public int? GenderTypeId { get; set; }
public List<FindPersonResultsViewModel> findPersonResultsViewModel { get; set; }

The List FindPersonResultsViewModel contains

public string FirstName { get; set; }
public string LastName { get; set; } 

At first on a Post I have the following data

public IActionResult FindPerson (FindPersonViewModel findPersonViewModel)

    Id : 4432
    EmailAddress:  "johnsmith@test.com"
    findPersonResultsViewModel  :  null 

So what I want to do is hydrate this list view model with some dummy data ( I will add real data later...

Seems like I'm having trouble with knowing how to do this, new up a list of this ?

findPersonViewModel.findPersonResultsViewModel.Add({  ??

List<FindPersonResultsViewModel> findPersonResultsViewModel = 
    new List<FindPersonResultsViewModel>();   
// How to add data , especially to that model that contains the list   findPersonViewModel  ??

标签: c# list poco
2条回答
姐就是有狂的资本
2楼-- · 2020-04-18 07:19

You just need to do this:

 findPersonViewModel.findPersonResultsViewModel = new List<FindPersonResultsViewModel>() { new FindPersonResultsViewModel { FirstName = "Zion", LastName = "Williamson" } };

I see no reason why this shouldn't work.

查看更多
混吃等死
3楼-- · 2020-04-18 07:25

If all you want to do is add some data to the null list on your object, you can do something like this:

var results = new List<FindPersonResultsViewModel>();
results.Add(new FindPersonResultsViewModel(){ ... all my properties ..});
findPersonViewModel.findPersonResultsViewModel = results;
查看更多
登录 后发表回答