0

我正在使用 Listview 使用模型绑定显示一些数据,并且我正在尝试对 listview 中与数据源中的外键列相关的列进行排序,即 book 列。

数据模型如下:

public class Book 
{
    public Book() {
        this.Factions = new HashSet<Faction>();
    }

    public int Id { get; set; }
    [Required]
    [MaxLength(50)]
    public string Title { get; set; }

   public virtual ICollection<Faction> Factions { get; set; }
}

public class Faction 
{    
    public int Id { get; set; }
    [Required]
    [MaxLength(50)]
    public string Name { get; set; }
    [MaxLength(10)]
    public string Abbreviation { get; set; }

    public int? BookId { get; set; }
    public virtual Book Book { get; set; }
}

这是显示 ListItem 标题的 HTML

<asp:ListView ID="FactionListView" runat="server"
ItemType="DCW.Models.Faction" DataKeyNames="Id"
SelectMethod="FactionGetData"
<LayoutTemplate>
    <table class="table table-hover table-bordered">
        <thead>
            <tr>
               <th>
                    <asp:LinkButton ID="FactionListViewName" runat="server" CommandName="Sort"
                        CommandArgument="Name">Name</asp:LinkButton></th>
                <th>
                    <asp:LinkButton ID="FactionListViewAbbreviation" runat="server" CommandName="Sort"
                        CommandArgument="Abbreviation">Abbreviation</asp:LinkButton></th>
                <th>
                    <asp:LinkButton ID="FactionListViewBook" runat="server" CommandName="Sort"
                        CommandArgument="Book">Book</asp:LinkButton></th>
            </tr>
        </thead>
        <tbody>
            <asp:PlaceHolder ID="itemPlaceholder" runat="server" />
        </tbody>
    </table>
</LayoutTemplate>

单击 Book LinkBut​​ton 时,我收到错误:Sys.WebForms.PageRequestManagerServerErrorException:DbSortClause 表达式必须具有可比顺序的类型。

如果我将 Linkbutton CommandArgument 更改为 Book.Id 或 it.Book.Title (我在其他一些帖子上阅读过可能有效),那么我会收到错误:Sys.WebForms.PageRequestManagerServerErrorException: Exception has been throwed by the target调用。

那么如何对模型绑定列表视图的相关列进行排序呢?

谢谢。

4

1 回答 1

0

因此,模型绑定似乎无法处理开箱即用的导航属性(外键)排序。我发现以下是我用来解决这个问题的方法: ASP.Net 4.5 Model Binding Sorting By Navigation Property

所以这:

<asp:LinkButton ID="FactionListViewBook" runat="server" CommandName="Sort"
                    CommandArgument="Book">Book</asp:LinkButton></th>

成为:

<asp:LinkButton ID="FactionListViewBook" runat="server" CommandName="Sort"
                    CommandArgument="Book.Title">Book</asp:LinkButton></th>

我的 SelectMethod 变成了:

public IQueryable<GameFaction> FactionGetData(string sortByExpression)
    {
        IQueryable<Faction> query = _context.Factions.Include(faction => faction.Book);

        sortByExpression = sortByExpression == null ? "Name" : sortByExpression;
        if (sortByExpression.EndsWith(" DESC"))
        {
            query = query.OrderByDescending(sortByExpression.SubString(0, sortByExpression.Length - 5));
        }
        else
        {
            query = query.OrderBy(sortByExpression);
        }

        return query;
    }

请注意,这使用了上面链接中提到的扩展方法,该方法最初来自这里: Dynamic LINQ OrderBy on IEnumerable<T>

于 2013-12-28T22:10:00.337 回答