【发布时间】:2009-01-02 17:09:29
【问题描述】:
我正在尝试将 ASP.net DropDownList 绑定到实体框架查询的结果,同时仍保持多层分离。 (即,我不希望我的 UI 代码包含查询详细信息,也不希望我的数据层代码具有 UI 依赖项。)我在 Page_Load 事件处理程序中的代码隐藏如下所示:
IEnumerable<Lookup> TypesLookup = Business.DocumentBO.GetDocumentTypes(_LookupTypeID);
DocTypeDropDownList.DataSource = TypesLookup;
DocTypeDropDownList.DataTextField = "Description";
DocTypeDropDownList.DataValueField = "LookupID";
DocTypeDropDownList.DataBind();
虽然我的数据代码看起来像这样(还有一个中间业务层,但还没有处理——只是一个传递。):
public static IEnumerable<Lookup> GetLookups(int LookupTypeID)
{
using (VLFDocumentEntities context = new VLFDocumentEntities())
{
IEnumerable<Lookup> l = (from c in context.Lookup
where c.LookupTypeID == LookupTypeID
select c);
return l;
}
}
当我到达 DocTypeDropDownList.DataBind(); 时,它会抛出带有消息“DocTypeDropDownList.DataBind();”的 ObjectDisposedException。谁能告诉我解决这个问题的最佳方法?
谢谢, 安迪
【问题讨论】:
-
Martin:如果我添加您建议的行,我会在该新行上收到 InvalidOperationException,并显示消息“对象无法分离,因为它未附加到 ObjectStateManager。”我已尝试搜索有关此新消息的信息,但一无所获。
-
好的,看来您必须分离列表/IEnumrable 中的每个对象,而不是 IEnumerable 本身。
标签: asp.net entity-framework data-binding 3-tier