Beispiel #1
0
        public ActionResult PageTable([ModelBinder(typeof(DataTablesBinder))] IDataTablesRequest requestModel)
        {
            var query = pagesService
                        .GetAllAndDeleted()
                        .Select(page => new PageFullViewModel()
            {
                Id             = page.Id,
                BookCatNum     = page.Book.CatalogueNumber,
                Number         = page.Number,
                Text           = page.Text,
                isDeleted      = page.isDeleted,
                DeletedOn      = page.DeletedOn,
                CreatedOn      = page.CreatedOn,
                ModifiedOn     = page.ModifiedOn,
                AuthorUsername = page.Author.UserName,
                AuthorId       = page.Author.Id
            })
                        .ToList();

            var totalCount = query.Count();

            // Apply filters for searching
            if (requestModel.Search.Value != string.Empty)
            {
                var value = requestModel.Search.Value.Trim();
                query = query.Where(page => page.Text.Contains(value)).ToList();
            }

            var filteredCount = query.Count();

            // Sorting
            var sortedColumns = requestModel.Columns.GetSortedColumns();
            var orderByString = String.Empty;

            foreach (var column in sortedColumns)
            {
                orderByString += orderByString != String.Empty ? "," : "";
                orderByString += (column.Data) + (column.SortDirection == Column.OrderDirection.Ascendant ? " asc" : " desc");
            }

            query = query.OrderBy(orderByString == string.Empty ? "Number asc" : orderByString).ToList();

            // Paging
            query = query.Skip(requestModel.Start).Take(requestModel.Length).ToList();

            var data = query.Select(page => new
            {
                Id = page.Id,
                //page / edit ? id = 13 & bookNum = 1
                Number         = "<a href=\"./edit?id=" + page.Number + "&bookNum=" + page.BookCatNum + "\">" + page.Number + "</a>",
                BookCatNum     = page.BookCatNum,
                Text           = page.Text,
                CreatedOn      = string.Format("{0:dd/MMM/yyyy}", page.CreatedOn),
                ModifiedOn     = string.Format("{0:dd/MMM/yyyy}", page.ModifiedOn),
                isDeleted      = page.isDeleted,
                DeletedOn      = string.Format("{0:dd/MMM/yyyy}", page.DeletedOn),
                AuthorUsername = page.AuthorUsername,
                AuthorId       = page.AuthorId
            }).ToList();

            return(Json(
                       new DataTablesResponse(requestModel.Draw, data, filteredCount, totalCount),
                       JsonRequestBehavior.AllowGet));
        }