c# - 如何使用基于 QueryString 的 ASP.NET ObjectDataSource 检索数据?

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

我是新的 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;
        }
}

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

enter image description here

感谢您提前提供的帮助。

最佳答案

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

的参数

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

关于c# - 如何使用基于 QueryString 的 ASP.NET ObjectDataSource 检索数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34680347/

相关文章:

c# - 在NEST中发送原始批量索引查询或仅以特定方式序列化文档

c# - 南希FX : how to get posted form value?

javascript - .NET 中的 js 包含文件

html - ASP.NET Div 布局 - CSS 粘性页脚

c# - 在复制和粘贴时查找丢失的文件和子目录及其文件列表?

c# - SQL 服务器 2008 : update query

javascript - Javascript 中 form.submit 和 window.open() 的区别

c# - ASP.NET PageMethods 和错误 "The HTTP verb POST used to access path is not allowed"

c# - 为什么绑定(bind) DropDownList 的功能不同于手动添加 ListItems?

asp.net - 如何设置 GridView 中编辑按钮列的宽度?