title: Viewmodel to allow sorting and filtering for mvc contrib grid
body: <p>In my attempt to enable sorting and filtering in conjunction with the MVC contrib. grid  I have decided to use a Viewmodel which contains the grid data plus the sorting and filtering information. I added a form under the grid and the Viewmodel is used to maintain the state of the form and to carry the data for the grid:</p>
  
  <pre><code>public class GridViewModel&lt;T&gt;
      {
          public int Page {get; set;} // current page of grid
          public string OrderBy {get; set;} // name of the column
          public bool Asc {get; set;} 
          public string Operation {get; set;} // SW - Startswith, C - Contains
          public string Column {get; set;} // name of the column where operation takes place
          public string Argument {get; set;} // argument for operation
          public int RowsPerPage {get; set;} // visible number of rows per page
  
          public List&lt;T&gt; Data {get; set;}
  
          public GridViewModel(List&lt;T&gt; Data)
          {
              this.Data = Data;
          }
          public GridViewModel()
          {
              Page = 1;
              Asc = true;
              RowsPerPage = 10;
              Argument = "";
          }
  
          public int getNumberofPages()
          {
              return (int) Math.Ceiling(Convert.ToDouble(Data.Count) / Convert.ToDouble(RowsPerPage));
          }
      }
  </code></pre>
  
  <p>The form is created using HTMLHelpers and by passing in the Viewmodel.</p>
  
  <p>The Viewmodel is created in a service method like this (not complete):</p>
  
  <pre><code>public GridViewModel&lt;Bla&gt; GetGridData(GridViewModel&lt;Bla&gt; GridViewModel)
          {
              IOrderedEnumerable&lt;Bla&gt; list = GetAllBlas();
  
              string Argument = GridViewModel.Argument.Trim();
              switch (GridViewModel.Column)
              {
                  case "Age":
                      switch (GridViewModel.Operation)
                      {
                          case "SW":
                              list = list.Where(c =&gt; c.Age.ToString().StartsWith(Argument)).OrderBy(c =&gt; c.ProposalDateTime);
                              break;
                          case "C":
                              list = list.Where(c =&gt; c.Age.ToString().Contains(Argument)).OrderBy(c =&gt; c.ProposalDateTime);
                              break;
                          default:
                              break;
                      }
                      break;
  
                  default:
                      break;
              }
  
              int start_index = (GridViewModel.RowsPerPage * GridViewModel.Page) - GridViewModel.RowsPerPage;
  
              switch (GridViewModel.OrderBy)
              {
                  case "Age":
                      list = GridViewModel.Asc == true ? list.OrderBy(c =&gt; c.Age) : list.OrderByDescending(c =&gt; c.Age);
                      break;
                  case "ProposalDateTime":
                      list = GridViewModel.Asc == true ? list.OrderBy(c =&gt; c.ProposalDateTime) : list.OrderByDescending(c =&gt; c.ProposalDateTime);
                      break;
                  case "UpdateDateTime":
                      list = GridViewModel.Asc == true ? list.OrderBy(c =&gt; c.UpdateDateTime) : list.OrderByDescending(c =&gt; c.UpdateDateTime);
                      break;
                  default:
                      list = GridViewModel.Asc == true ? list.OrderBy(c =&gt; c.ProposalDateTime) : list.OrderByDescending(c =&gt; c.ProposalDateTime);
                      break;
              }
  
              int number_of_rows = list.Count();
              if (start_index &gt; number_of_rows)
              {
                  start_index = 0;
              }
  
              GridViewModel.Data = list.Skip&lt;Bla&gt;((int)start_index).Take&lt;Bla&gt;((int)GridViewModel.RowsPerPage).ToList();
  
              return GridViewModel;
          }
  </code></pre>
  
  <p>This works but does not feel right yet ...</p>
  
  <p>I am just wondering whether this code could be improved – has someone done something similar? Any feedback would be very much appreciated. Many thanks in advance.</p>
  
  <p>Best wishes,</p>
  
  <p>Christian</p>