我正在做ASP.NET MVC 4
项目。我有ViewModel
:
public class SectionModel
{
public Menu Menu { get; set; }
public IList<Document> Documents { get; set;}
}
那么在我的行动中,我有:
Menu menu = unitOfWork.MenuRepository.GetById(Id);
IList<Document> docs = unitOfWork.DocumentRepository.GetBy(x => x.MenuID == menu.MenuID).ToList();
SectionModel model = new SectionModel();
model.Menu = menu;//???
model.Documents = docs;//???
因此menu
,docs
并从数据库中获取。当我创建I的新实例时,SectionModel
我想传递值,但是我不确定什么是正确的方法。实际上,分配单个对象menu
和对象的方法是什么IList<Document> docs
?
我在想,model.Menu.Add(menu)
然后像下面的一些foreach:
foreach (var doc in docs)
{
model.Documents.Add(doc);
}
但我不确定这是否是正确的方法。
如果您的代码应与其他项目互操作,但我始终使用下面的代码,则建议使用接口 List<T>
你必须改变这个
public class SectionModel
{
public Menu Menu { get; set; }
public List<Document> Documents { get; set;}
}
和使用
model.Documents.AddRange(docs);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句