我的asp:gridview无法显示IQueryable结果中的对象

时间:2017-06-09 07:08:04

标签: asp.net .net entity-framework gridview dbcontext

情形:

         platform: Visual studio 2012,

         framework: .net framework 4.5

问题:

我的asp有问题:GridView。当我尝试从IQueryable结果中提取数据时,只返回原始类型。原始类型显示在gridview中,但不显示来自CitizenModel的对象SpecieModel和ObjectRole。

关于可能发生的事情的任何想法?

我在下面有CitizenModel类

[Browsable(false)]
    [Key]
    public int Id { get; set; }

    [Browsable(true)]
    [Required(ErrorMessage = "The name field is required")]
    public string Name { get; set; }

    [Browsable(true)]
    [Required(ErrorMessage = "The specie field is required")]
    public SpecieModel SpecieType { get; set; }

    [Browsable(true)]
    public RoleModel ObjectRole { get; set; }

    [NotMapped]
    public int Specie
    {
        get
        {
            if (this.SpecieType != null)
                return this.SpecieType.Id;
            else
                return 0;
        }
    }

    [NotMapped]
    public int Role
    {
        get
        {
            if (this.ObjectRole != null)
                return this.SpecieType.Id;
            else
                return 0;
        }
    }

我的背景

public class ImperialDBContext :DbContext
{
    public ImperialDBContext()
        : base("DBImperial11")
    {
    }

    //protected override void OnModelCreating(DbModelBuilder modelBuilder)
    //{
    //    // other code 
    //    Database.SetInitializer<ImperialDBContext>(null);
    //    // more code
    //}

    public DbSet<CitizenModel> Citizens { get; set; }

    public DbSet<RoleModel> Roles { get; set; }

    public DbSet<LogErroModel> LogErros { get; set; }

}

我的asp gridview:

<asp:GridView ID="gridDados" runat="server" ItemType="Imperial.Class.Entidade.CitizenModel" SelectMethod="CarregarGrid" DataKeyNames="Id" OnRowCommand="gridDados_RowCommand">
    <Columns>
        <asp:TemplateField ShowHeader="false">
            <ItemTemplate>
                <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Excluir" OnClientClick="return confirm('Deseja excluir ?')" CommandArgument='<%#: Item.Id %>' Text="Excluir"></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField ShowHeader="false">
            <ItemTemplate>
                <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Editar" CommandArgument='<%#: Item.Id %>' Text="Editar"></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>

    </Columns>
</asp:GridView>

我的方法来自gridview的carregarGrid

public IQueryable<CitizenModel> CarregarGrid()
    {
        try
        {
            return ImperialFacade.Instancia.LoadCitizenDatas();
        }
        catch (Exception ex)
        {
            ImperialFacade.Instancia.InserirLogErro(new LogErroModel(ex.Message));
            throw ex;
        }
    }

在我的门面:

public IQueryable<CitizenModel> LoadCitizenDatas()
    {
        try
        {
            return new CitizenNegocio().LoadDatas();
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }

0 个答案:

没有答案