views:

119

answers:

1

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:

public class GridViewModel<T>
    {
        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<T> Data {get; set;}

        public GridViewModel(List<T> 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));
        }
    }

The form is created using HTMLHelpers and by passing in the Viewmodel.

The Viewmodel is created in a service method like this (not complete):

public GridViewModel<Bla> GetGridData(GridViewModel<Bla> GridViewModel)
        {
            IOrderedEnumerable<Bla> list = GetAllBlas();

            string Argument = GridViewModel.Argument.Trim();
            switch (GridViewModel.Column)
            {
                case "Age":
                    switch (GridViewModel.Operation)
                    {
                        case "SW":
                            list = list.Where(c => c.Age.ToString().StartsWith(Argument)).OrderBy(c => c.ProposalDateTime);
                            break;
                        case "C":
                            list = list.Where(c => c.Age.ToString().Contains(Argument)).OrderBy(c => 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 => c.Age) : list.OrderByDescending(c => c.Age);
                    break;
                case "ProposalDateTime":
                    list = GridViewModel.Asc == true ? list.OrderBy(c => c.ProposalDateTime) : list.OrderByDescending(c => c.ProposalDateTime);
                    break;
                case "UpdateDateTime":
                    list = GridViewModel.Asc == true ? list.OrderBy(c => c.UpdateDateTime) : list.OrderByDescending(c => c.UpdateDateTime);
                    break;
                default:
                    list = GridViewModel.Asc == true ? list.OrderBy(c => c.ProposalDateTime) : list.OrderByDescending(c => c.ProposalDateTime);
                    break;
            }

            int number_of_rows = list.Count();
            if (start_index > number_of_rows)
            {
                start_index = 0;
            }

            GridViewModel.Data = list.Skip<Bla>((int)start_index).Take<Bla>((int)GridViewModel.RowsPerPage).ToList();

            return GridViewModel;
        }

This works but does not feel right yet ...

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.

Best wishes,

Christian

A: 

The closest thing to an answer can be found here (proposed by: Sergey Prokhorenko):

http://sprokhorenko.blogspot.com/2009/12/dedicated-to-my-wife.html

It is jqgrid specific but provided me with some ideas.

Christian

csetzkorn