2

我在这里的实体框架中很新,并尝试创建一个简单的列表框控件,但列表框出来是空的。请帮助并告诉我我在这里做错了什么。

DAL(实体框架):

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; }        
 }
}

控制器:

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();
 }

看法:

@Html.ListBox("ListClients", (IEnumerable<SelectListItem>) ViewBag.Clients)
4

1 回答 1

1

您需要 ToList() 该查询来解决它。执行选择不会解析表达式。

ViewBag.Clients = clients.ToList();

于 2013-01-24T17:43:19.603 回答