【问题标题】:GridView binding with custom property in EntityObject derived class fails in ASP.NET与 EntityObject 派生类中的自定义属性绑定的 GridView 在 ASP.NET 中失败
【发布时间】:2013-04-19 12:36:56
【问题描述】:

我有一个实体框架层,并从该层扩展了一个类。代码如下:

public partial class FeatureMaster : EntityObject
{
    public string ParentFeatureName
    {
        get
        {
            return (from r in Global.dc.FeatureMasters
                    where r.FeatureId == FeatureParentId
                    select r).SingleOrDefault().FeatureName;
        }
        set
        {
            FeatureParentId = (from r in Global.dc.FeatureMasters
                               where r.FeatureName == value
                               select r).SingleOrDefault().FeatureId;
            ReportPropertyChanged(("ParentFeatureName"));
            ReportPropertyChanged(("FeatureParentId"));
        }
    }
}

现在我希望在我的 ASP.NET 页面中使用相同的内容。我的 ASP.NET 页面的代码是:

    <%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true"
    CodeBehind="ListFeatureMaster.aspx.cs" Inherits="Backend.ListFeatureMaster" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="cpMain" runat="server">
    <asp:GridView ID="grdRecords" runat="server" AllowPaging="True" AllowSorting="True"
        AutoGenerateColumns="False" DataKeyNames="FeatureId" DataSourceID="EntityDataSource1">
        <Columns>
            <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
            <asp:BoundField DataField="FeatureId" HeaderText="FeatureId" ReadOnly="True" SortExpression="FeatureId" />
            <asp:BoundField DataField="FeatureName" HeaderText="FeatureName" SortExpression="FeatureName" />
            <asp:BoundField DataField="FeatureParentId" HeaderText="FeatureParentId" SortExpression="FeatureParentId" />
            <asp:BoundField DataField="FeatureDescription" HeaderText="FeatureDescription" SortExpression="FeatureDescription" />
            <asp:TemplateField HeaderText="ParentFeatureName" SortExpression="ParentFeatureName">
                <EditItemTemplate>
                    <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="EntityDataSource1"
                        AppendDataBoundItems="true" DataTextField="ParentFeatureName" DataValueField="ParentFeatureName"
                        SelectedValue='<%# Bind("ParentFeatureName") %>'>
                    </asp:DropDownList>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("ParentFeatureName") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    <asp:EntityDataSource ID="EntityDataSource1" runat="server" ConnectionString="name=BackendEntities"
        DefaultContainerName="BackendEntities" EnableFlattening="False" EntitySetName="FeatureMasters"
        EntityTypeFilter="FeatureMaster" EnableDelete="True" EnableUpdate="True">
    </asp:EntityDataSource>
        <asp:EntityDataSource ID="EntityDataSource2" runat="server" ConnectionString="name=BackendEntities"
        DefaultContainerName="BackendEntities" EnableFlattening="False" EntitySetName="FeatureMasters"
        EntityTypeFilter="FeatureMaster" EnableDelete="True" EnableUpdate="True">
    </asp:EntityDataSource>
</asp:Content>

问题是当我执行这段代码时,它在第一次加载和导航时运行良好,但是当我尝试更新记录时,我得到了错误。请帮我解决同样的问题。

错误信息说:

A property named 'ParentFeatureName' was not found on the entity during an insert, update, or delete operation. Check to ensure that properties specified as binding expressions are available to the data source.

【问题讨论】:

    标签: asp.net entity-framework c#-4.0


    【解决方案1】:

    我认为问题在于您没有将自定义属性附加到您的实体,然后将其保存。你可以看看Adding customer property to entity class

    希望对你有帮助。

    【讨论】:

    • @minhchat_vo - 它不起作用,也没有帮助我。请给我一些其他选择。
    【解决方案2】:

    我得到了解决方案。

    我更改了 GridView 的定义并向其中添加了 OnRowUpdating 事件。 ASPX 页面代码现在如下所示:

    <asp:GridView ID="grdRecords" runat="server" AllowPaging="True" AllowSorting="True"
        AutoGenerateColumns="False" DataKeyNames="FeatureId" DataSourceID="EntityDataSource1" OnRowUpdating="grdRecords_RowUpdating">
    

    在我的 .aspx.cs 文件中,我添加了事件处理程序的代码。相同的代码如下所示:

            protected void grdRecords_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int primaryKey = Convert.ToInt32(grdRecords.DataKeys[e.RowIndex].Value);
    
            FeatureMaster existingRecord = Global.dc.FeatureMasters.First(r => r.FeatureId == primaryKey);
            existingRecord.FeatureName = e.NewValues["FeatureName"].ToString();
            existingRecord.ParentFeatureName = ((DropDownList)(grdRecords.Rows[e.RowIndex].FindControl("DropDownList1"))).SelectedValue.ToString();
            existingRecord.FeatureDescription = e.NewValues["FeatureDescription"].ToString();
    
            Global.dc.SaveChanges();
    
            grdRecords.EditIndex = -1;
    
            e.Cancel = true;
        }
    

    【讨论】:

      猜你喜欢
      • 2012-05-19
      • 2012-06-02
      • 1970-01-01
      • 2013-06-20
      • 2013-05-01
      • 2010-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多