使用ReleatedEntity
我已经获得了相关实体的列表。但我只想将Id + Name粘贴在一个视图包中,以便Razor可以绑定下拉列表。
问题:
如何仅将Id
,Name
实体作为List
,以便我可以将其绑定到razor *中的ASP MVC 5中的DropDowns
; 并将其作为 Viewbag.Attribute,即ViewBag.EntityName
编辑1:简化我的代码
foreach (var relatedFkEntity in (((IEntityWithRelationships)EntityType.FirstOrDefault())
.RelationshipManager.GetAllRelatedEnds()))
{
// EntityNameHere <- how to stick the Id and Names list there from the EF entities
viewbag.**Entity Name Here** = dropdownListWithId&Name; //??
entity.Include(relatedEntity.TargetRoleName).ToList();
}
答案 0 :(得分:1)
请您查看以下代码,请进行未经测试的更改
foreach (var relatedFkEntity in (((IEntityWithRelationships)EntityType.FirstOrDefault ()).RelationshipManager.GetAllRelatedEnds()))
{
viewbag.drpList = new SelectList(entity.Include(relatedEntity.TargetRoleName).ToList(), "Id", "Name");
}