我正在使用 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 LinkButton 时,我收到错误:Sys.WebForms.PageRequestManagerServerErrorException:DbSortClause 表达式必须具有可比顺序的类型。
如果我将 Linkbutton CommandArgument 更改为 Book.Id 或 it.Book.Title (我在其他一些帖子上阅读过可能有效),那么我会收到错误:Sys.WebForms.PageRequestManagerServerErrorException: Exception has been throwed by the target调用。
那么如何对模型绑定列表视图的相关列进行排序呢?
谢谢。