locked
My asp:gridview can't show object from IQueryable result RRS feed

  • Question

  • User1730550683 posted

    scenario:

                 platform: Visual studio 2012,

                 framework: .net framework 4.5

    Problem:

    I am having a problem with my asp:GridView. When I try to bring the data from a IQueryable result but only primitive types are returning. The primitive types are showing in the gridview, but not object SpecieModel and ObjectRole from CitizenModel.

    Any ideas about what can be happening?

    I have CitizenModel class below

    [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;
                }
            }

    My Context

    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; }
    }

    My 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>

    My method carregarGrid from gridview

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

    Friday, June 9, 2017 6:55 AM

All replies

  • User991499041 posted

    Hi almeidalucas,

    <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>

    There are a couple of important concepts in this markup code for you to notice. First, notice that a value is set for the SelectMethod property in the GridView element. This value specifies the name of the method that is used for retrieving data for the GridView. You will create this method in the next step. Second, notice that the ItemType property is set to the Student class that you created earlier.

    return ImperialFacade.Instancia.LoadCitizenDatas();

    For this case, try providing the Include clause, the data is retrieved using eager loading, which means all of the related data is retrieved through a single query of the database. Check https://docs.microsoft.com/en-us/aspnet/web-forms/overview/presenting-and-managing-data/model-binding/retrieving-data.

    Regards,

    zxj

    Monday, June 12, 2017 9:02 AM