This is a migrated thread and some comments may be shown as answers.

Binding grid to viewmodel containing a list

3 Answers 89 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Shane P
Top achievements
Rank 1
Shane P asked on 27 Feb 2012, 09:35 PM
I need some guidance in binding a grid to my view model.

I have a model and view model that look like

public class CardTransactionModel
   {
       public int CardTransactionId { get; set; }
       public Guid UserId { get; set; }
       public decimal Amount { get; set; }
       public DateTime TransactionDate { get; set; }
       public int CardPaymentId { get; set; }
   }
 
   public class CardTransactionPaymentViewModel
   {
       public string CardNumber { get; set; }
       public List<CardTransactionModel> CardTransactionModel { get; set; }
   }

I am binding my grid to my viewmodel
@{Html.Telerik().Grid(Model)
        .Name("Grid")
        .Selectable()
        .Columns(columns =>
        {
            columns.Bound(o => o.CardNumber);
        })
        .DataKeys(keys =>
        {
            keys.Add(p => p.CardNumber);
        })
        .Scrollable()
        .Sortable()
        .Pageable()
        .Filterable()
        .Footer(true)
        .Render();
}


How can I bind the columns to not only show CardNumber but all of the other properties contained in the list?

Any help would be great.


Thank you



3 Answers, 1 is accepted

Sort by
0
Dadv
Top achievements
Rank 1
answered on 28 Feb 2012, 09:32 AM
Hi,

Look at this :http://demos.telerik.com/aspnet-mvc/grid/hierarchyajax 

an other way could be to adapt your ViewModel to have a single list :


public class CardTransactionModel
   {
       
       
public string CardNumber { getset; } 
public int CardTransactionId { getset; }
       public Guid UserId { getset; }
       public decimal Amount { getset; }
       public DateTime TransactionDate { getset; }
       public int CardPaymentId { getset; }
   }
 
   public class CardTransactionPaymentModel : CardTransactionModel 
   {
       public string CardNumber { getset; }
   }

   public class CardTransactionPaymentViewModel
   {
       public List<CardTransactionPaymentModelCardTransactionPaymentsgetset; }
   }


After you just have to apply a grouping command on CardNumber.

Regards,




0
Shane P
Top achievements
Rank 1
answered on 28 Feb 2012, 10:01 AM
Thanks Dadv,

I don't want to have a grouped effect applied, I was hoping for the grid to appear more as a flat file vs a grouped looked.
0
Dadv
Top achievements
Rank 1
answered on 28 Feb 2012, 10:16 AM
Then when you create the list, make the first CardNumber have a value and the other to null


CardNumber  CardTransactionId  UserId  Amount  
123456               5                                  1            1000.0
   6                                  1              500.5
   7                                  5              300.5 
654321               5                                  1              110.0 
   6                                  1              500.5  

but you will loose the sort ability
Tags
Grid
Asked by
Shane P
Top achievements
Rank 1
Answers by
Dadv
Top achievements
Rank 1
Shane P
Top achievements
Rank 1
Share this question
or