locked
How to load EntitySet navigation property and use it as a DataTextField in a DropDownList binding? RRS feed

  • Question

  • Hello :)

    I'm new to ASP.NET developpment  and use of Entity Framework.

    I have a model that contains 3 entities:

    - Commercial which inherits from SiteMember
    - SiteMember which inherits from DefaultUser
    - DefaultUser which contains firstName and LastName properties.

    I would like to create a dropdownlist which contains the list of all Commercial entries from my database and displays their firstName + LastName.

    I tried the following code :

    List<Commercial> commerciaux = context.Commercial.Include("SiteMember").Include("DefaultUser").ToList();
    
    if (commerciaux != null)
    {
       ddlCommerciaux.DataSource = commerciaux;
       ddlCommerciaux.DataTextField = "SiteMember.DefaultUser.LastName";
       ddlCommerciaux.DataValueField = "Id";
       ddlCommerciaux.DataBind();
    }
    
    

    It doesn't work. I get an error which explains the EntitySet Commercial does not contain DefaultUser as a navigation property.

    I also tried with just the SiteMember navigation property and its property "SiteMember.Login" but it also doesn't work.

    What should I write to make it work?

    Thank you :)

    Regards.

    Saturday, June 4, 2011 9:43 AM

Answers

  • Hi,

    I dont' rememember exactly how the DropDownList binding works, it may not support propertytrees for binding.

    What you could do if you want to bind is to do the following:

    var commerciaux = context.Commercial.Include("SiteMember.DefaultUser").Select(row => new { Id = row.Id, Name = row.SiteMember.DefaultUser.FirstName + " " + row.SiteMember.DefaultUser.LastName}).ToList();
    
    if (commerciaux != null)
    {
      ddlCommerciaux.DataSource = commerciaux;
      ddlCommerciaux.DataTextField = "Name";
      ddlCommerciaux.DataValueField = "Id";
      ddlCommerciaux.DataBind();
    }
    
    
    The select will then give you an list of a anonymous type that contains the Id and FullName, this can again be bound into your DropDownList.

    Hope this helps!

     


    --Rune
    Saturday, June 4, 2011 1:45 PM
  • Waitasecod ... the Commercial inherits or contains SiteMember? If it inherits then it has property .firstName and .lastName directly without the need to go through any navigation properties.

    Next thing is ... if you only need the commerciaux list for this binding, then you'd most probably do better if you did not select the whole Commercial objects, but rather just the Id and LastName you need.

     

    var commerciaux = context.Commercial.Select(c => new {Id = c.Id, Name = c.SiteMember.DefaultUser.LastName}).ToList();

    if (commerciaux != null) {

      ddlCommerciaux.DataSource = commerciaux;

      ddlCommerciaux.DataTextField = "Name";

      ddlCommerciaux.DataValueField = "Id";

    ...

     

    This way you load less data from the database.

     

    Jenda


    ----------------------------------
    http://jendaperl.blogspot.com
    A Perl developer in the world of C#
    Sunday, June 5, 2011 9:42 AM

All replies

  • Hi!

    The reason for this is that Commercial doesn't contain DefaultUser, but SiteMember does. So try .Include("SiteMember.DefaultUser") and it will work better.

    A another tip, if you are working with inheritance, you can make Commercial inherit Sitemember which inherits DefaultUser. It will save you for some work.

    This blog post describes this for you: http://www.robbagby.com/entity-framework/entity-framework-modeling-table-per-type-inheritance/

     

    Hope this helps!


    --Rune
    Saturday, June 4, 2011 10:38 AM
  • Hi!

    Thank you for your reply :)

     

    I trie the .Include("SiteMember.DefaultUser") and in the ddl.DataTextField, "SiteMember.DefaultUser.LastName", but I get the error "MyContext.Commercial does not contain a property called SiteMember.DefaultUser.LastName".

    I tried with "DefaultUser.LastName" and "LastName" as DataTextField value but none of them works.

    edit :

    I found the solution :)

    I replaced the binding by :

    foreach (Commercial c in commerciaux)
    {
       ddlCommerciaux.Items.Add(new ListItem(c.SiteMember.DefaultUser.FirstName + c.SiteMember.DefaultUser.LastName, c.Id.ToString()));
    }

    It works :)

    I don't know where the problem comes from with the dataTextField though =(

    • Edited by h4seo Saturday, June 4, 2011 1:30 PM found a solution
    Saturday, June 4, 2011 1:14 PM
  • Hi,

    I dont' rememember exactly how the DropDownList binding works, it may not support propertytrees for binding.

    What you could do if you want to bind is to do the following:

    var commerciaux = context.Commercial.Include("SiteMember.DefaultUser").Select(row => new { Id = row.Id, Name = row.SiteMember.DefaultUser.FirstName + " " + row.SiteMember.DefaultUser.LastName}).ToList();
    
    if (commerciaux != null)
    {
      ddlCommerciaux.DataSource = commerciaux;
      ddlCommerciaux.DataTextField = "Name";
      ddlCommerciaux.DataValueField = "Id";
      ddlCommerciaux.DataBind();
    }
    
    
    The select will then give you an list of a anonymous type that contains the Id and FullName, this can again be bound into your DropDownList.

    Hope this helps!

     


    --Rune
    Saturday, June 4, 2011 1:45 PM
  • Waitasecod ... the Commercial inherits or contains SiteMember? If it inherits then it has property .firstName and .lastName directly without the need to go through any navigation properties.

    Next thing is ... if you only need the commerciaux list for this binding, then you'd most probably do better if you did not select the whole Commercial objects, but rather just the Id and LastName you need.

     

    var commerciaux = context.Commercial.Select(c => new {Id = c.Id, Name = c.SiteMember.DefaultUser.LastName}).ToList();

    if (commerciaux != null) {

      ddlCommerciaux.DataSource = commerciaux;

      ddlCommerciaux.DataTextField = "Name";

      ddlCommerciaux.DataValueField = "Id";

    ...

     

    This way you load less data from the database.

     

    Jenda


    ----------------------------------
    http://jendaperl.blogspot.com
    A Perl developer in the world of C#
    Sunday, June 5, 2011 9:42 AM
  • Any update? Was your question solved? Please remember to mark helpful replies as Answered.

    Best Regards,


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, June 7, 2011 7:24 AM