【发布时间】:2013-12-22 20:35:59
【问题描述】:
我正在使用 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调用的目标。
那么如何对模型绑定列表视图的相关列进行排序?
谢谢。
【问题讨论】:
标签: listview sorting model-binding entity-framework-6 commandargument