【问题标题】:How to retrieve the data using ASP.NET ObjectDataSource based on the QueryString?如何使用基于 QueryString 的 ASP.NET ObjectDataSource 检索数据?
【发布时间】:2016-04-13 07:56:18
【问题描述】:

我是新的 ASP.NET Web 窗体开发人员,我正在尝试使用带有存储库模式的 ObjectDataSource 开发 n 层应用程序。到目前为止,我做得很好,但我正在努力根据 QueryString 的值检索其中一个页面中的数据。

我有以下数据库架构:

Product Table: Id, Title, CategoryId
Category Table: Id, Title

用户必须访问列出所有类别的页面。然后,当他选择其中一个类别时,他将被重定向到另一个页面,该页面根据所选类别列出产品。我仍在为此苦苦挣扎,找不到解决方法。

ASP.NET 代码:

<asp:ListView ID="productList" runat="server"
                            DataSourceID="odsProduct" DataKeyNames="Id">
                            <LayoutTemplate>
                                <ul class="cbp-ig-grid">
                                    <asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder>
                                </ul>
                            </LayoutTemplate>
                            <ItemTemplate>
                                <li class=" animated7">
                                    <a href="ProductDetails.aspx?productID=<%#Eval("Id") %>">
                                        <img src="../Assets/upload/Products/<%#Eval("ImagePath") %>" width="350" height="190">
                                        <h3 class="cbp-ig-title"><%#Eval("Name") %></h3>
                                        <span class="cbp-ig-category"><%#Eval("Description") %></span></a>
                                </li>
                            </ItemTemplate>
                            <EmptyDataTemplate>
                                <span>No data was returned.</span>
                            </EmptyDataTemplate>
                        </asp:ListView>
                        <asp:ObjectDataSource ID="odsProduct" runat="server"
                            TypeName="ThinkSafetyFirst_DatabaseFirst.BLL.ProductBL"
                            DataObjectTypeName="ThinkSafetyFirst_DatabaseFirst.DAL.Product"
                            SelectMethod="GetProducts">
                            <SelectParameters>
                                <asp:QueryStringParameter QueryStringField="categoryId" Name="CategoryId " />
                            </SelectParameters>
                        </asp:ObjectDataSource>

这是 ProductRepository 类的 C# 代码:

public class ProductRepository : IDisposable, IProductRepository
    {
        private readonly TestContext dbContext = new TestContext();

        public IEnumerable<TTSF_Product> GetProducts()
        {
            return dbContext.TTSF_Product.Include("TTSF_Category").ToList();
        }


        public TTSF_Product GetProduct(int id)
        {
            TTSF_Product proObj = dbContext.TTSF_Product.Find(id);
            return proObj;
        }
}

您能告诉我为什么我仍然收到以下错误吗?

提前感谢您的帮助。

【问题讨论】:

  • 您是否假设使用 productID 而不是 categoryID 作为查询字符串?从您的 href 值来看,您使用的是 productID。

标签: c# asp.net webforms repository-pattern objectdatasource


【解决方案1】:

ObjectDataSource 中,您将SelectMethod 定义为GetProducts,但您的存储库中的方法GetProducts 没有名为CategoryId 的参数

您应该为以CategoryId 作为参数的GetProducts 方法添加一个重载。

【讨论】:

  • 那么Include("TTSF_Category") 不会为这样的情况做任何事情吗?
  • 是的,问题是您正在调用一个方法,其参数在您的存储库对象中不存在
  • 感谢您的澄清。因此,EntityFramework 没有任何功能可以让我根据作为类别 ID 的外键的值来检索产品列表?另外,如果您能给我提供一个代码 sn-p 用于使我能够根据 QueryString 的值检索数据的方法,我将不胜感激
  • 您的意思是加载即:CategoryID 为 2 的产品?
  • 是的,我就是这个意思。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-30
  • 2022-01-11
  • 1970-01-01
  • 1970-01-01
  • 2016-04-07
  • 2021-06-18
相关资源
最近更新 更多