在模型绑定列表视图中排序外键列

时间:2013-12-22 20:35:59

标签: listview sorting model-binding entity-framework-6 commandargument

我正在使用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:异常被抛出调用的目标。

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

感谢。

1 个答案:

答案 0 :(得分: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>