导航属性上的 DropDownList DataTextField
我需要以下方面的帮助。
我从实体框架数据上下文中获取对象列表。
var list = context.EntityA;
EntityA 是主对象(包含主键),但有一个名为“EntityALanguages”的导航属性,其中包含特定于语言的属性。
现在我想将列表绑定到下拉列表,并且需要从下拉列表中设置 DataValueField 和 DataTextField 属性。
我如何将 DataTextField 设置为导航属性的属性,例如:
this.ddl.DataValueField = "GUID";
this.ddl.DataTextField = "EntityALanguages.ShortDescription";
编辑: 导航属性“EntityALanguages”是一个集合,因此 EntityA -> EntityALanguages 是 1-n 关系
i need some help with the following.
i get a list of objects from the Entity Framework data context.
var list = context.EntityA;
the EntityA is the main object (contains the primary key), but has a navigation property called "EntityALanguages", which contains language specific properties.
now i want to bind the list to a dropdownlist and need so set DataValueField and DataTextField properties from the dropdownlist.
how can i set the DataTextField to a property of a navigation property, something like:
this.ddl.DataValueField = "GUID";
this.ddl.DataTextField = "EntityALanguages.ShortDescription";
Edit:
The navigation property "EntityALanguages" is a collection, so EntityA -> EntityALanguages is a 1-n relation
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通过使用 var list = context.EntityA; ,您的导航属性将被延迟加载。尝试 var list = context.EntityA.Include("EntityALanguages"); 这样您的导航属性就会出现。
By using
var list = context.EntityA;
your navigation properties will be lazy loaded. Tryvar list = context.EntityA.Include("EntityALanguages");
so your navigation propery will be present.DropDownList 可能不支持用于绑定的属性树。
如果要绑定,您可以执行以下操作:
var items = context.Entity.Include("EntityALanguages").Select(row => new { Id = row.GUID, Name = row.EntityALanguages.ShortDescription }).ToList();
ddl.DataTextField = "名称";
ddl.DataValueField = "Id";
DropDownList may not support propertytrees for binding.
What you could do if you want to bind is to do the following:
var items = context.Entity.Include("EntityALanguages").Select(row => new { Id = row.GUID, Name = row.EntityALanguages.ShortDescription}).ToList();
ddl.DataTextField = "Name";
ddl.DataValueField = "Id";
在您的实体 EntityALanguages 中,您可以添加一个像这样的只读属性
In your entity EntityALanguages you could add a readonly property like this