数据绑定 ASP.net DropDownList 与实体框架

发布于 2024-07-10 21:04:44 字数 1075 浏览 5 评论 0原文

我试图将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

把时间冻结 2024-07-17 21:04:44

您不需要将对象与上下文分离吗? 例如:

IEnumerable<Lookup> l = (from c in context.Lookup
                        where c.LookupTypeID == LookupTypeID
                        select c);
foreach (Lookup lookup in l)
  context.Detach(lookup);
return l;

Don't you have to detach the objects from the context? E.g:

IEnumerable<Lookup> l = (from c in context.Lookup
                        where c.LookupTypeID == LookupTypeID
                        select c);
foreach (Lookup lookup in l)
  context.Detach(lookup);
return l;
原野 2024-07-17 21:04:44

为什么不直接使用 List<> ?

public static List<Lookup> GetLookups(int LookupTypeID)
{
    using (VLFDocumentEntities context = new VLFDocumentEntities())
    {
        return (from c in context.Lookup
                    where c.LookupTypeID == LookupTypeID
                    select c).ToList();
    }
}

Why don't you just use a List<>?

public static List<Lookup> GetLookups(int LookupTypeID)
{
    using (VLFDocumentEntities context = new VLFDocumentEntities())
    {
        return (from c in context.Lookup
                    where c.LookupTypeID == LookupTypeID
                    select c).ToList();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文