2009-11-30 5 views
0

드롭 다운에사용은 강력하게 다음 주어진 .... 다음 클래스 감안할 목록

namespace IMTool.Data 
{ 
    public partial class AllContracts 
    { 
     internal class Metadata 
     { 
      public int ContractId { get; set; } 
      [Required] 
      public string Name { get; set; } 
     } 
    } 
} 

바인딩 및 데이터가 아닌 문자열을 입력했습니다.

using (var context = new IMToolDataContext()) 
{ 
    ddlContracts.DataValueField = "ContractId"; 
    ddlContracts.DataTextField = "Name"; 
    ddlContracts.DataSource = context 
     .AllContracts 
     .OrderBy(o => o.Name); 
    ddlContracts.DataBind(); 
} 

어떻게 데이터 드롭 다운 목록과 데이터 텍스트 필드를 강력하게 입력 할 수 있습니까? 기본적으로 문자열을 사용하고 싶지 않지만 엔티티의 열 이름을 사용하고 싶습니다. LinqToSql을 사용하고 있습니다. (잘 PLinqo는 코드 더미 집합으로 내 데이터 계층을 생성합니다.) 아무도 도와 줄 수 있습니까?

using (var context = new IMToolDataContext()) 
{ 
    ddlContracts.DataValueField = GetKeyColumnName(typeof(Metadata)); 
    ddlContracts.DataTextField = GetNameColumnName(typeof(Metadata)); 
    ddlContracts.DataSource = context 
     .AllContracts 
     .OrderBy(o => o.Name); 
    ddlContracts.DataBind(); 
} 

편집 : 열

답변

2

internal class Metadata 
{ 
    [MappingColumn (Type="Key")] 
    public int ContractId { get; set; } 
    [Required] 
    [MappingColumn (Type="Name")] 
    public string Name { get; set; } 
} 

이처럼 DDL을이 서명

string GetKeyColumName(Type type) //will perfom a loop on the type properties custom attribute and return the first with the type Key 

string GetNameColumnName(Type type)//will perfom a loop on the type properties custom attribute and return the first with the type Name 

와 두 가지 방법을 만들고 채우는 할 수있는 사용자 지정 특성 만들기 내가 참조하는 속성은 Linq의 속성이 아니라 yourcunstom 속성입니다. 좋습니다 MappingColumn이라고 선언해야합니다.

public class MappingColumnAttribute : System.Attribute 
{ 

    public string Type {get;set;} 
}