简体   繁体   English

Navigation属性上的DropDownList DataTextField

[英]DropDownList DataTextField on Navigation property

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. EntityA是主要对象(包含主键),但是具有名为“ EntityALanguages”的导航属性,其中包含特定于语言的属性。

now i want to bind the list to a dropdownlist and need so set DataValueField and DataTextField properties from the dropdownlist. 现在我想将列表绑定到下拉列表,并且需要从下拉列表设置DataValueField和DataTextField属性。

how can i set the DataTextField to a property of a navigation property, something like: 如何将DataTextField设置为导航属性的属性,例如:

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 编辑:导航属性“ EntityALanguages”是一个集合,所以EntityA-> EntityALanguages是1-n关系

In your entity EntityALanguages you could add a readonly property like this 在您的实体EntityALanguages中,您可以添加一个只读属性,如下所示

public readonly string EntityALanguagesShortDescription
{
    get { return this.EntityALanguages.ShortDescription; }
}

By using var list = context.EntityA; 通过使用var list = context.EntityA; your navigation properties will be lazy loaded. 您的导航属性将被延迟加载。 Try var list = context.EntityA.Include("EntityALanguages"); 尝试var list = context.EntityA.Include("EntityALanguages"); so your navigation propery will be present. 因此您的导航属性将会存在。

DropDownList may not support propertytrees for binding. DropDownList可能不支持绑定属性树。

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(); var items = context.Entity.Include(“ EntityALanguages”)。Select(row => new {ID = row.GUID,Name = row.EntityALanguages.ShortDescription})。ToList();

ddl.DataTextField = "Name"; ddl.DataTextField =“名称”; ddl.DataValueField = "Id"; ddl.DataValueField =“ Id”;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM