只将List <id,name>从Entity放入Viewbag。“EntityName”

时间:2017-02-16 03:53:12

标签: c# asp.net asp.net-mvc entity-framework dropdown

使用ReleatedEntity我已经获得了相关实体的列表。但我只想将Id + Name粘贴在一个视图包中,以便Razor可以绑定下拉列表。

问题: 如何仅将IdName实体作为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();                     
 }

1 个答案:

答案 0 :(得分:1)

请您查看以下代码,请进行未经测试的更改

foreach (var relatedFkEntity in (((IEntityWithRelationships)EntityType.FirstOrDefault    ()).RelationshipManager.GetAllRelatedEnds()))
{

      viewbag.drpList = new SelectList(entity.Include(relatedEntity.TargetRoleName).ToList(), "Id",     "Name");
                         
}