【发布时间】:2023-03-30 14:40:01
【问题描述】:
使用ReleatedEntity 我已经获得了相关实体的列表。但我只想将 Id+Name 粘贴在 viewbag 中,以便 Razor 可以绑定下拉列表。
问题:
如何仅获取实体的Id、Name 作为List,以便我可以在 ASP MVC 5 in razor* 中将其绑定到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();
}
【问题讨论】:
-
这里似乎有很多自定义工作,所以很难看出哪里出了问题。
-
@Shoe 首先,感谢您的关注。我可以简化它。我的代码获取实体列表。对于 foreach 实体,我只想获取
key/Id和Name,以便我可以从 viewbag 连接到 ASP MVC 剃须刀中的dropdownControl。
标签: c# asp.net asp.net-mvc entity-framework dropdown