jqgrid List of objects (with sub objects) as datas

2019-06-03 03:58发布

问题:

Can someone give me a hand with this please.

i have 2 classes, Foo and Moo

public class Foo
{
    public int Id { get; set; }
    public int price { get; set; }
    public String code { get; set; }
     public Moo moo { get; set; }

public Foo()
{
}
 }


 public class MOO
 {
     public int Id { get; set; }
     public String firstname { get; set; }
     public String surname { get; set; }

     public MOO()
     {
     }
 }

I have a list of FOOs now

List<Foo> foolist

I can make it a datasource for my jqgrid. and do columns for price and code, but I cant use the sub fields

I would love to be able to do something like this

<trirand:JQGridColumn DataField="moo.firstname" Searchable="true" /> 

any ideas? Thanks!

回答1:

Basically, you can change your return list. You create a new object. Example: InfoFoo. you set its property while using Foo and Moo classes. Then return this class, List.

public class InfoFoo
{
  public int Id { get; set; }
  public int price { get; set; }
  public String code { get; set; }
  public int MooId { get; set; }
  public String MooFirstname { get; set; }
  public String MooSurname { get; set; }
}

for setting InfoFoo properties,

List<InfoFoo> ListInfoFoo=new List<InfoFoo>(); 
foreach(var foo in List<Foo>)
 {
   var infoFoo=new InfoFoo();
   infoFoo.Id=foo.Id;
   infoFoo.Price=foo.Price;
   infoFoo.code=foo.code;
   infoFoo.MooId=foo.Moo.Id;
   infoFoo.MooFirstName=foo.Moo.firstName;
   infoFoo.MooSurname=foo.Moo.surname;  
   ListInfoFoo.Add(infoFoo);
  }

 return ListInfoFoo;