【问题标题】:Put only the List<id,Name> from Entity into Viewbag."EntityName"仅将 Entity 中的 List<id,Name> 放入 Viewbag。“EntityName”
【发布时间】:2023-03-30 14:40:01
【问题描述】:

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

问题: 如何仅获取实体的IdName 作为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/IdName,以便我可以从 viewbag 连接到 ASP MVC 剃须刀中的 dropdownControl

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


【解决方案1】:

你能看看下面的代码吗,请修改一下,这还没有测试过

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

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

【讨论】:

  • 嗨,它显示错误我试图在 dotnetfiddle.com 运行它,两件事,在 viewbag 上的 entitytype,我正在尝试 viewbag.+this.GetType().Name.ToSTring() = new selectlist.... 我想将实体的名称添加到视野包
猜你喜欢
  • 2013-11-04
  • 1970-01-01
  • 1970-01-01
  • 2016-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多