【发布时间】:2019-06-11 22:32:32
【问题描述】:
我有一个之前的问题,它显示了我的模型的外观,并且它正在添加 FAKE 数据。 Add to Existing Model based on a POCO with need to add to List<T>
现在我想添加真实数据,我想知道如何做到这一点。我应该还是需要循环 result ??
public IActionResult FindPerson (FindPersonViewModel findPersonViewModel)
{
var firstName = findPersonViewModel.FirstName;
var middleName = findPersonViewModel.MiddleName;
var lastName = findPersonViewModel.LastName;
var emailAddress = findPersonViewModel.EmailAddress;
var genderTypeId = findPersonViewModel.GenderTypeId;
// GET REAL DATA
using (AzEdsIdentityContext context = new AzEdsIdentityContext(AzEdsIdentityContext.Options))
{
var result = context.FindPerson(firstName, lastName, genderTypeId);
// for loop on the result to hydrate new List<FindPersonResultsViewModel>() ?
}
// Note: here is exactly how I hydrated the model with fake data
findPersonViewModel.findPersonResultsViewModel = new List<FindPersonResultsViewModel>()
{ new FindPersonResultsViewModel { AZEDID = 33423432, PersonID = 3534454, FirstName = "John", LastName = "Williamson", MiddleName = "K", ExistInContactManager = false, ActionType = true, ContactType = "Principal", DOB = "5/1/1985", PhysicalAddress = "123 main st. mesa, az.", PreferredEmail = "john@aol.com", PreferredPhone = "602-393-4443"},
new FindPersonResultsViewModel { AZEDID = 33423432, PersonID = 3534454, FirstName = "Jon", LastName = "Williamson", MiddleName = "K", ExistInContactManager = false, ActionType = true, ContactType = "Principal", DOB = "5/1/1985", PhysicalAddress = "123 main st. mesa, az.", PreferredEmail = "john@aol.com", PreferredPhone = "602-393-4443"},
};
}
【问题讨论】:
-
只需将集合属性设置为您从上下文中获得的结果。如果模型不匹配,那么您需要Select projection。
-
@Jasen 我不确定它是否会匹配,你能给我举个例子吗?
-
您从
context.FindPerson()中得到了什么?这与您的“假”数据有何不同? -
很多相同的字段,我会做一个foreach循环并创建一个新列表吗?
-
您不需要遍历结果。请参阅链接的答案。
标签: c# asp.net list asp.net-core viewmodel