gpt4 book ai didi

c# - ASP.Net 4.5 模型绑定(bind)按导航属性排序

转载 作者:太空狗 更新时间:2023-10-30 00:53:24 25 4
gpt4 key购买 nike

全部,

我有一个包含以下列的 GridView 。分页效果很好,但不是排序。每次我单击“类别”列以按类别排序时,我都会收到此错误:

实例属性“Category.CategoryName”没有为类型“ESA.Data.Models.Entity.Project”定义

此错误陈述不正确,因为 gridview 能够正确显示该列。

这里是选择方法

    public IQueryable<Project> getProjects()
{
ApplicationServices objServices = new ApplicationServices();
IQueryable<Project> lstProject;
lstProject = objServices.getProjects();
return lstProject;
}

有什么建议吗?

    <asp:GridView ID="grdProject" runat="server" ShowHeader="true" 
AutoGenerateColumns="false" CellPadding="2" CellSpacing="2"
ItemType="ESA.Data.Models.Entity.Project"
SelectMethod="getProjects"
DataKeyNames="ProjectID"
AllowSorting="true"
AllowPaging="true"
PageSize="5">
<Columns>
<asp:BoundField DataField="ProjectID" HeaderText="ID " ItemStyle-Width="10" />
<asp:BoundField DataField="Category.CategoryName" HeaderText="Category" SortExpression="Category.CategoryName" />
<asp:BoundField DataField="ProjectName" HeaderText="Project Name" ItemStyle-Width="300" />
<asp:BoundField DataField="Status.StatusName" HeaderText="Status" SortExpression="Status.StatusName" />
<asp:BoundField DataField="AddedByUser.UserName" HeaderText="Added By" ItemStyle-Width="120" />
<asp:BoundField DataField="AddedDate" HeaderText="Added Date" ItemStyle-Width="90" DataFormatString="{0:d}" />
</Columns>
</asp:GridView>

最佳答案

我在使用 Listview 控件时遇到了类似的问题。我是这样解决的。

首先,我使用了 Marc Gravell 发表的这篇文章中的代码 Dynamic LINQ OrderBy on IEnumerable<T>

在我的 Listview 的“OnSorting”事件中,我添加了以下代码。

protected void lv_Sorting(object sender, ListViewSortEventArgs e)
{
e.Cancel = true;
ViewState["OrderBy"] = e.SortExpression;
lvList.DataBind();
}

我添加了一个相当标准的方法来捕获这个 sortdirection 列表

public SortDirection sortDirection
{
get
{
if (ViewState["sortdirection"] == null)
{
ViewState["sortdirection"] = SortDirection.Ascending;
return SortDirection.Ascending;
}
else if ((SortDirection)ViewState["sortdirection"] == SortDirection.Ascending)
{
ViewState["sortdirection"] = SortDirection.Descending;
return SortDirection.Descending;
}
else
{
ViewState["sortdirection"] = SortDirection.Ascending;
return SortDirection.Ascending;
}
}
set
{
ViewState["sortdirection"] = value;
}
}

在我的 Listview 中,Selectmethod 看起来像这样(使用 Marc 的扩展方法)

public IQueryable<SomeObject> GetObjects([ViewState("OrderBy")]String OrderBy = null)
{
var list = GETSOMEOBJECTS();
if (OrderBy != null)
{
switch (sortDirection)
{
case SortDirection.Ascending:
list = list.OrderByDescending(OrderBy);
break;
case SortDirection.Descending:
list = list.OrderBy(OrderBy);
break;
default:
list = list.OrderByDescending(OrderBy);
break;
}
}
return list;
}

我还没有用 GridView 尝试过,但我很确定它会工作得一样。

编辑这是应该工作的 linq 扩展类的示例

public static class LinqExtensions
{
public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> source, string property)
{
return ApplyOrder<T>(source, property, "OrderBy");
}
public static IOrderedQueryable<T> OrderByDescending<T>(this IQueryable<T> source, string property)
{
return ApplyOrder<T>(source, property, "OrderByDescending");
}
public static IOrderedQueryable<T> ThenBy<T>(this IOrderedQueryable<T> source, string property)
{
return ApplyOrder<T>(source, property, "ThenBy");
}
public static IOrderedQueryable<T> ThenByDescending<T>(this IOrderedQueryable<T> source, string property)
{
return ApplyOrder<T>(source, property, "ThenByDescending");
}
static IOrderedQueryable<T> ApplyOrder<T>(IQueryable<T> source, string property, string methodName)
{
string[] props = property.Split('.');
Type type = typeof(T);
ParameterExpression arg = Expression.Parameter(type, "x");
Expression expr = arg;
foreach (string prop in props)
{
// use reflection (not ComponentModel) to mirror LINQ
PropertyInfo pi = type.GetProperty(prop);
expr = Expression.Property(expr, pi);
type = pi.PropertyType;
}
Type delegateType = typeof(Func<,>).MakeGenericType(typeof(T), type);
LambdaExpression lambda = Expression.Lambda(delegateType, expr, arg);

object result = typeof(Queryable).GetMethods().Single(
method => method.Name == methodName
&& method.IsGenericMethodDefinition
&& method.GetGenericArguments().Length == 2
&& method.GetParameters().Length == 2)
.MakeGenericMethod(typeof(T), type)
.Invoke(null, new object[] { source, lambda });
return (IOrderedQueryable<T>)result;
}
}

只需在页面上添加一个 using 'whatevernamespaceyouused' 就可以了。

关于c# - ASP.Net 4.5 模型绑定(bind)按导航属性排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16550231/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com