对列表视图中的现有项目进行排序

Sort existing items in listview

我正在尝试对我拥有的列表视图中的项目进行排序,这些项目是通过列表视图 ASPX 声明中的 SelectMethod 填充到列表视图中的。

<asp:ListView ID="productList" runat="server" 
            DataKeyNames="ProductID" GroupItemCount="4"
            ItemType="StoreTest.Models.Product" SelectMethod="GetProducts" >

该方法只是执行一个 linq 查询,然后 returns 查询返回到列表视图,这是列表视图的内部工作方式 class 并且工作正常。问题是我有一个下拉列表,其中包含每个项目的不同变量,用户可以使用此 DDL 对列表视图进行排序。

我在 DDL 上放置了一个 OnSelectedIndexChanged 方法,它执行与原始查询相同的查询,但随后根据所需的排序机制进一步对其进行排序。问题是我无法用这个新查询替换列表视图中的现有项目。我的代码如下:

        productList.Items.Clear();
        var _db = new StoreTest.Models.SiteContext();
        IQueryable<Product> query = _db.Products;
        switch(sortList.SelectedValue)
        {
            case "Price Asc":
                query = query.OrderBy(o=> o.UnitPrice);
                break;
            case "Price Des":
                query = query.OrderByDescending(o => o.UnitPrice);
                break;
            case "Name Asc":
                query = query.OrderBy(o => o.ProductName);
                break;
            case "Name Des":
                query = query.OrderByDescending(o => o.ProductName);
                break;
            case "Product ID Asc":
                query = query.OrderBy(o => o.ProductID);
                break;
            case "Product ID Des":
                query = query.OrderByDescending(o => o.ProductID);
                break;
        }
            productList.DataSource = query;
            productList.DataBind();

这给了我一个错误 "DataSource or DataSourceID cannot be defined on 'productList' when it uses model binding",我该如何解决这个问题。我尝试将查询中的每个产品转换为 ListViewItem,但类型不兼容,将它们转换为数组是行不通的。

public IQueryable<Product> GetProducts([QueryString("id")] int? categoryID)
    {
        var _db = new StoreTest.Models.SiteContext();
        IQueryable<Product> query = _db.Products;
        if (categoryID.HasValue && categoryID > 0)
        {
            query = query.Where(p => p.CategoryID == categoryID);
            Session["categoryid"] = categoryID;
        }
        return query;
    }

谢谢。

编辑 解决方案: 以编程方式绑定两次,删除 aspx 中的绑定并将初始绑定更改为 protected void Page_Load(object sender, EventArgs e) { IQueryable itemList = GetProducts(); productList.DataSource = itemList.ToList(); productList.DataBind(); }

    public IQueryable<Product> GetProducts()
    {
        int categoryID = -1;
        if(Request.QueryString["id"]!=null)
        {
            categoryID = Convert.ToInt32(Request.QueryString["id"]);
        }
        var _db = new StoreTest.Models.SiteContext();
        IQueryable<Product> query = _db.Products;
        if (categoryID!=-1 && categoryID > 0)
        {
            query = query.Where(p => p.CategoryID == categoryID);
            Session["categoryid"] = categoryID;
        }
        return query;
    }

第二个绑定工作正常:

productList.DataSource = query.ToList();
        productList.DataBind();

问题的出现是因为您正在尝试 声明式绑定(ASPX 中的 SelectMethod)以及 代码绑定 (productList.DataSource = 查询)。当您使用 SelectMethod 进行绑定时,您使用的是声明式绑定(这又是您的模型绑定 - 您显示的异常表示相同)。

我建议您也通过代码进行初始绑定,可能在页面加载时删除 SelectMethod。这样,您的两种方法都将使用代码绑定,并且应该可以工作。