我有一个类似的模型:
public class PurchaseOrderPreliminaryCSVModel
{
public string Item1 { get; set; }
public string Item2 { get; set; }
public string Item3 { get; set; }
public string Item4 { get; set; }
public IList<PreliminaryPurchaseOrderDesignNameViewModel> PreliminaryDesignNames { get; set; } = new List<PreliminaryPurchaseOrderDesignNameViewModel>();
public class PreliminaryPurchaseOrderDesignNameViewModel
{
public List<IEnumerable> DesignName { get; set; }
}
}
所以我将项目对象填充为:
List<PurchaseOrderPreliminaryCSVModel> result = new List<PurchaseOrderPreliminaryCSVModel>();
try
{
if (Path.GetExtension(fileName).Equals(".csv"))
{
using (MemoryStream memory = new MemoryStream(model.PreliminaryFile.File))
{
using (var reader = new StreamReader(memory))
using (var csv = new CsvReader(reader))
{
var record = new PurchaseOrderPreliminaryCSVModel();
var records = csv.EnumerateRecords(record);
foreach (var r in records)
{
PurchaseOrderPreliminaryCSVModel tempResult = new PurchaseOrderPreliminaryCSVModel
{
Item1 = r.Item1,
Item2 = r.Item2,
Item3 = r.Item3,
Item4 = r.Item4,
};
result.Add(tempResult);
}
}
}
因此,我将ViewModel列表放入了另一个我执行的方法中:
var designNames = _purchaseOrderRepository.GetPreliminaryDesignNames(fileName);
我的问题是:如何将模型PreliminaryDesignNames { get; set; }
填充到foreach
中,就像这样:
result.Add(designNames);
但是指定填充该视图模型。我怎样才能做到这一点?问候
答案 0 :(得分:0)
您可以使用. operator从 PurchaseOrderPreliminaryCSVModel 类型(代码中的tempResult)的实例访问 PreliminaryDesignNames 属性
...
foreach (var r in records)
{
PurchaseOrderPreliminaryCSVModel tempResult = new PurchaseOrderPreliminaryCSVModel
{
Item1 = r.Item1,
Item2 = r.Item2,
Item3 = r.Item3,
Item4 = r.Item4,
};
var designNames = _purchaseOrderRepository.GetPreliminaryDesignNames(fileName);
tempResult.PreliminaryDesignNames.AddRange(designNames);
result.Add(tempResult);
}
...