我在GridView中遇到DynamicFields的问题。我正在尝试使用来自相关实体的属性获取字段,例如Order实体中的“Customer.Name”,但总是得到错误“表'Order'没有名为'Customer.Name'的列。 “
我将客户实体包含在linq查询中。
如果我通过BoundField更改DynamicField,一切正常。
编辑:这是代码......
<asp:GridView ID="grvActivities" runat="server"
Caption="<%$ Resources:QuasarApp, MyOpenActivities %>" AutoGenerateColumns="false"
DataKeyNames="ActivityId" ItemType="Quasar.Model.Activity"
ShowHeaderWhenEmpty="True" AllowSorting="True"
ShowFooter="True" CssClass="table table-striped table-bordered table-hover table-condensed table-responsive"
SelectMethod="grvActivities_GetData"
UpdateMethod="grvActivities_UpdateItem"
OnRowCommand="grvActivities_RowCommand">
<Columns>
<asp:HyperLinkField ControlStyle-CssClass="nounderline fa fa-edit"
DataNavigateUrlFields="ActivityId" DataNavigateUrlFormatString="~/Activities/Edit.aspx?RecordId={0}"
ItemStyle-Width="25px" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle" />
<asp:TemplateField ShowHeader="false">
<ItemTemplate>
<asp:LinkButton ID="Complete" runat="server" CommandName="Complete" CommandArgument='<%# Eval("ActivityId") %>'
CssClass="nounderline fa fa-check-square-o" ToolTip="<%$ Resources:QuasarApp, MarkAsCompleted %>" />
</ItemTemplate>
</asp:TemplateField>
<asp:DynamicField DataField="StartDate" DataFormatString="{0:g}" />
<asp:DynamicField DataField="DueDate" DataFormatString="{0:g}" />
<asp:DynamicField DataField="EndDate" DataFormatString="{0:g}" />
<asp:DynamicField DataField="Name" />
<asp:DynamicField DataField="Activity.Customer.Name" />
<asp:DynamicField DataField="Lead" />
<asp:DynamicField DataField="PriorityLevel" />
<asp:DynamicField DataField="ActivityType" />
<asp:DynamicField DataField="Status" />
<asp:DynamicField DataField="CreatedBy" />
<asp:ButtonField ButtonType="Link" CommandName="Delete" ControlStyle-CssClass="nounderline fa fa-eraser"
ItemStyle-Width="25px" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle" />
</Columns>
</asp:GridView>
代码隐藏:
public IQueryable<Activity> grvActivities_GetData()
{
var query = _uow.Activities.GetMany(a =>
a.IsActive
&& a.AccountManagerId == currentAccountManager
&& a.EndDate == null,
q => q.OrderByDescending(s => s.StartDate),
includeProperties: "Customer, Lead, ActivityType");
return query;
}
EDIT2: 嗨,我发现这个专栏:
<asp:TemplateField HeaderText="<%$ Resources:QuasarApp, Customer %>">
<ItemTemplate>
<asp:Label Text="<%# Item.Customer != null ? Item.Customer.Name : String.Empty %>" runat="server" />
</ItemTemplate>
</asp:TemplateField>
它工作正常,但我正在失去所有排序等功能。为什么导航属性在数据源中出现,DynamicField找不到这个属性?
答案 0 :(得分:0)
对于导航属性,您应该只使用:
<asp:DynamicField DataField="Customer" />
动态数据将显示您班级的第一个字符串属性。或者你可以强制注释:
[DisplayColumn("Name")]
public class Customer
但是如果您使用EF6,则需要安装提供程序 [http://blogs.msdn.com/b/webdev/archive/2014/02/28/announcing-the-release-of-dynamic-data-provider-and-entitydatasource-control-for-entity-framework-6.aspx][1]
并在global.asax.cs中注册您的模型:
void Application_Start(object sender, EventArgs e)
{
MetaModel DefaultModel = new MetaModel();
DefaultModel.RegisterContext(new Microsoft.AspNet.DynamicData.ModelProviders.EFDataModelProvider(
() => new YOURCONTEXT()),
new ContextConfiguration { ScaffoldAllTables = false });
}
最后设置page_init代码:
protected void Page_Init()
{
grvActivities.SetMetaTable(MetaTable.GetTable(typeof(Activity)));
}