数据绑定 ASP.net DropDownList 与实体框架
我试图将 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(); 时,它抛出一个 ObjectDisposeException 并带有消息“DocTypeDropDownList.DataBind();”。 谁能告诉我解决这个问题的最佳方法?
谢谢, 安迪
I'm trying to bind an ASP.net DropDownList to the results of an entity framework query, while still maintaining multi-tier separation. (i.e. I don't want my UI code to contain query details, nor my Data Layer code to have UI dependencies.) My code-behind in the Page_Load event handler looks like this:
IEnumerable<Lookup> TypesLookup = Business.DocumentBO.GetDocumentTypes(_LookupTypeID);
DocTypeDropDownList.DataSource = TypesLookup;
DocTypeDropDownList.DataTextField = "Description";
DocTypeDropDownList.DataValueField = "LookupID";
DocTypeDropDownList.DataBind();
While my data code looks like this (there's an intermediate business layer as well, but there no processing there as yet -- just a pass-through.):
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;
}
}
When I get to the DocTypeDropDownList.DataBind();, it throws an ObjectDisposedException with the message "DocTypeDropDownList.DataBind();". Can anyone advise me on the best way to tackle this?
Thanks,
Andy
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不需要将对象与上下文分离吗? 例如:
Don't you have to detach the objects from the context? E.g:
为什么不直接使用 List<> ?
Why don't you just use a List<>?