2

I am very new in Entity Framework here and try to create a simple listbox control but the listbox comes out empty. Please help and tell me what I did wrong here.

DAL (Entity Framework):

namespace DataAccess
{
using System;
using System.Collections.Generic;

public partial class Client
{
    public Client()
    {
        this.AnotoPGCFiles = new HashSet<AnotoPGCFile>();
        this.Client1 = new HashSet<Client>();
        this.Client11 = new HashSet<Client>();
        this.ClientBillingSetups = new HashSet<ClientBillingSetup>();
        this.ClientDataStores = new HashSet<ClientDataStore>();
        this.ClientDepartments = new HashSet<ClientDepartment>();
        this.ClientFileNames = new HashSet<ClientFileName>();
        this.ClientReports = new HashSet<ClientReport>();
    }

    public int ID { get; set; }
    public string Name { get; set; }
    public string ContactName { get; set; }
    public string ContactEmail { get; set; }
    public string PathToLogo { get; set; }
    public bool Disabled { get; set; }
    public System.DateTime Created { get; set; }
    public int TimeZoneID { get; set; }

    public virtual AnotoLicensePool AnotoLicensePool { get; set; }
    public virtual ICollection<AnotoPGCFile> AnotoPGCFiles { get; set; }
    public virtual ICollection<Client> Client1 { get; set; }
    public virtual Client Client2 { get; set; }
    public virtual ExternalSystemDemographicConfiguration  ExternalSystemDemographicConfiguration { get; set; }
    public virtual ICollection<Client> Client11 { get; set; }
    public virtual Client Client3 { get; set; }
    public virtual TimeZone TimeZone { get; set; }        
 }
}

Controller:

public ActionResult Index()
 {
  ViewData["ReportFormat"] = "html";
  ViewBag.Title = "Home Page";

  var db = new DataAccess.Client();
  IEnumerable<SelectListItem> clients = db.Client1
        .Select(c => new SelectListItem
        {
              Value = c.ID.ToString(),
              Text = c.Name
        });

  ViewBag.Clients = clients;
  return View();
 }

View:

@Html.ListBox("ListClients", (IEnumerable<SelectListItem>) ViewBag.Clients)
1
  • 1
    Check your code that fills data into Client1 collection. Place a breakpoint at the statement that selects values into this collection and check if the values are getting filled. Also, check the query that is hit to backend using SQL Server profiler. Examine the query to find out what is going wrong. Commented Jan 24, 2013 at 13:03

1 Answer 1

1

You need to ToList() that query to resolve it. Doing the select doesn't resolve the expression.

ViewBag.Clients = clients.ToList();

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.