Add to Existing Model based on a POCO with need to add to List<T>

。_饼干妹妹 提交于 2019-12-20 04:29:12

问题


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  ??

回答1:


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.




回答2:


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;


来源:https://stackoverflow.com/questions/56534700/add-to-existing-model-based-on-a-poco-with-need-to-add-to-listt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!