Viewmodel 允许对 mvc contrib 网格进行排序和过滤

发布于 2024-09-17 17:18:59 字数 3624 浏览 5 评论 0原文

我尝试与 MVC contrib 一起启用排序和过滤。网格 我决定使用包含网格数据以及排序和过滤信息的视图模型。我在网格下添加了一个表单,Viewmodel 用于维护表单的状态并携带网格的数据:

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

该表单是使用 HTMLHelpers 并通过传入 Viewmodel 创建的。

Viewmodel 是在这样的服务方法中创建的(不完整):

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

这可以工作,但感觉还不正确......

我只是想知道这段代码是否可以改进 - 有人做过类似的事情吗?任何反馈将不胜感激。非常感谢。

最好的祝愿,

克里斯蒂安

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

街角卖回忆 2024-09-24 17:18:59

最接近答案的东西可以在这里找到(由:Sergey Prokhorenko 提出):

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

它是 jqgrid 特定的,但为我提供了一些想法。

基督教

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文