我是新的 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;
}
}
您能告诉我为什么我仍然收到以下错误吗?
感谢您提前提供的帮助。
最佳答案
在 ObjectDataSource
中,您将 SelectMethod
定义为 GetProducts
,但存储库中的方法 GetProducts
没有有一个名为 CategoryId
您应该为以 CategoryId
作为参数的 GetProducts
方法添加重载。
关于c# - 如何使用基于 QueryString 的 ASP.NET ObjectDataSource 检索数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34680347/