How to do pagination after every 10 comments in C# - c#

I have a list of comments formed. The client wants a new page after every 10 comments, so comment 11 will be on page 2, and so on for as many pages as they get. The commets are formed on a .ashx page becuase of an issue I had with the regular .aspx.cs page. Simply put, how do I accomplish this?
Here is the code of the .ashx page:
public void ProcessRequest (HttpContext context)
{
// ****************************************
if (context.Request["postform"] == "1")
{
videomessage myVideoMessage = new videomessage();
myVideoMessage.video_id = context.Request["video_id"];
myVideoMessage.first_name_submitter = context.Request["first_name_submitter"];
myVideoMessage.last_initial_submitter = context.Request["last_initial_submitter"];
myVideoMessage.message = context.Request["message"];
myVideoMessage.status = "0";
myVideoMessage.Save();
}
// ****************************************
// ****************************************
StringBuilder myStringBuilder = new StringBuilder();
// PULL VIDEOMESSAGES FOR VIDEO_ID
videomessage[] myCommentsList = new videomessage().Listing("video_id", context.Request["video_id"], "entry_date" , "DESC");
// FORM COMMENTS IF MORE THAN ONE COMMENT EXISTS
foreach (videomessage tmpMessage in myCommentsList)
{
if (tmpMessage.status == "0" || tmpMessage.status == "1")
{
myStringBuilder.Append("<div class=\"comment_box\">");
myStringBuilder.Append("<p class=\"comment_date\">");
myStringBuilder.Append(Utility.FormatShortDate(tmpMessage.entry_date) + " " + tmpMessage.first_name_submitter + " " + tmpMessage.last_initial_submitter + "." + "</p>");
if (!String.IsNullOrEmpty(tmpMessage.message))
{
myStringBuilder.Append("<p>" + tmpMessage.message + "</p>");
myStringBuilder.Append("</div>");
}
}
}
string return_str = myStringBuilder.ToString();
// IF NO COMMENTS RETURN THIS
if( String.IsNullOrEmpty(return_str) ) return_str = "<p>No comments currently exist for this video.</p>";
// ****************************************
// RETURN STRING
context.Response.ContentType = "text/plain";
context.Response.Write(return_str);
}
I know there will need to be some math involved as well as assigning it to variables, but I am still new to .NET so any help would be appreciated.
Thanks in advance!

using Linq
const int pageSize = 10;
var paginatedComments = comments.Skip((page ?? 0) * pageSize).Take(pageSize).ToList();
(*) page is input from request
Maybe a little outdated, but have a look at the nerddinner tutorial (http://aspnetmvcbook.s3.amazonaws.com/aspnetmvc-nerdinner_v1.pdf), which describes a pagination principle for MVC using a custom PaginatedList class
public class PaginatedList<T> : List<T> {
public int PageIndex { get; private set; }
public int PageSize { get; private set; }
public int TotalCount { get; private set; }
public int TotalPages { get; private set; }
public PaginatedList(IQueryable<T> source, int pageIndex, int pageSize) {
PageIndex = pageIndex;
PageSize = pageSize;
TotalCount = source.Count();
TotalPages = (int) Math.Ceiling(TotalCount / (double)PageSize);
this.AddRange(source.Skip(PageIndex * PageSize).Take(PageSize));
}
public bool HasPreviousPage {
get {
return (PageIndex > 0);
}
}
public bool HasNextPage {
get {
return (PageIndex+1 < TotalPages);
}
}
}
Alternative solution would be using a jquery approach (see http://www.jquery4u.com/plugins/10-jquery-pagination-plugins), but I have no experience with either of the plugins suggested on this site.

Have a look at this
currentPage=1
NextPageFirstRecord=(currrentpage-1)*noRecordsPerPage
pageCount=totalrecords/noRecordsPerPage
Hope this helps

Related

Extension method with generic input and output object casting

This question is mostly not a problem help call, but an invitation for the comparison between developers on contemporary, more advanced and clean development methods.
Here I explain how I've resolved this problem and show you a working code example. Anyway, I have some doubts about my solution. I'm really curios if into nature exists a more elegant way to achieve the same code optimization.
I've started from the issue when I had two different controllers with mostly same response models except Items property type.
Precision: We need to have dedicated and not shared response models for each controller. In my opinion it helps in the future, when the only one controller's response have to be changed without creating side effects for the others.
I've started from the problem when I had two different controllers with mostly same response models except Items property type.
Here them are:
namespace Webapi.Models.File {
public class Response {
public FileItem [] Items { get; set; }
public int Page { get; set; }
public int TotalPages { get; set; }
}
public class FileItem {
...
}
}
namespace Webapi.Models.User {
public class Response {
public UserItem [] Items { get; set; }
public int Page { get; set; }
public int TotalPages { get; set; }
}
public class UserItem {
...
}
}
The first model is populated in this way:
using FileModel = Webapi.Models.File;
private FileModel.Response CreateItemsPage(List<FileModel.FileItem> items, int page) {
int maxItemsPerPage = 50;
var chunks = items.Select((v, i) => new { Value = v, Index = i })
.GroupBy(x => x.Index / maxItemsPerPage).Select(grp => grp.Select(x => x.Value));
int totalChunks = chunks.Count();
if(totalChunks == 0) {
return null;
}
page = page > 1 ? page : 1;
page = totalChunks < page ? 1 : page;
return new FileModel.Response() {
Items = (chunks.ToArray())[page-1].ToArray(),
Page = page,
TotalPages = totalChunks
};
}
And the second method is exactly the same except input (List<UserModel.UserItem>) and output (UserModel.Response) types:
using UserModel = Webapi.Models.User;
private UserModel.Response CreateItemsPage(List<UserModel.UserItem> items, int page) {
int maxItemsPerPage = 50;
var chunks = items.Select((v, i) => new { Value = v, Index = i })
.GroupBy(x => x.Index / maxItemsPerPage).Select(grp => grp.Select(x => x.Value));
int totalChunks = chunks.Count();
if(totalChunks == 0) {
return null;
}
page = page > 1 ? page : 1;
page = totalChunks < page ? 1 : page;
return new UserModel.Response() {
Items = (chunks.ToArray())[page-1].ToArray(),
Page = page,
TotalPages = totalChunks
};
}
Having two and then even more cloned methods inside my webapi's controllers isn't a good perspective and I have resolved this by creating two ObjectExtensions methods.
The first one just reassign properties from source to target object. Both logically must have same properties inside (name and type):
public static TTarget AssignProperties<TTarget, TSource>(this TTarget target, TSource source) {
foreach (var targetProp in target.GetType().GetProperties()) {
foreach (var sourceProp in source.GetType().GetProperties()) {
if (targetProp.Name == sourceProp.Name && targetProp.PropertyType == sourceProp.PropertyType) {
targetProp.SetValue(target, sourceProp.GetValue(source));
break;
}
}
}
return target;
}
The second receives target and source objects, creates internally an anonymous one, then reassigns properties from it by using the previous extension method AssignProperties to the target object (need this because cannot access generic objects properties directly):
public static TTarget CreateItemsPage<TTarget, TSource>(this TTarget target, List<TSource> items, int page = 1) {
int maxItemsPerPage = 50;
var chunks = items.Select((v, i) => new { Value = v, Index = i })
.GroupBy(x => x.Index / maxItemsPerPage).Select(grp => grp.Select(x => x.Value));
int totalChunks = chunks.Count();
if(totalChunks == 0) {
return target;
}
page = page > 1 ? page : 1;
page = totalChunks < page ? 1 : page;
var source = new {
Items = (chunks.ToArray())[page-1].ToArray(),
Page = page,
TotalPages = totalChunks
};
target = target.AssignProperties(source);
return target;
}
And here is the usage:
...
var items = _filesService.ListAllUserFiles(userId, requestData.SearchText);
if(pages.Count() == 0)
return BadRequest();
return Ok(new FileModel.Response().CreateItemsPage(items, requestData.Page));
...
Some code examples would be appreciated. Thank you!
By opening a discussion on Reddit I came to the following solution that allows me to keep models separate and remove the inefficient AssignProperties method.
Interface:
public interface IPaginationResponse<TItem> {
TItem[] Items { get; set; }
int Page { get; set; }
int TotalPages { get; set; }
}
Models examples:
public class Response: IPaginationResponse<Info> {
public Info [] Items { get; set; }
public int Page { get; set; }
public int TotalPages { get; set; }
...
}
public class Response: IPaginationResponse<UserFile> {
public UserFile [] Items { get; set; }
public int Page { get; set; }
public int TotalPages { get; set; }
...
}
public class Response: IPaginationResponse<UserItem> {
public UserItem [] Items { get; set; }
public int Page { get; set; }
public int TotalPages { get; set; }
...
}
Now finally I've removed AssignProperties from CreateItemsPage extension method. Thanks to where TTarget : IPaginationResponse<TSource> I can assign values directly to TTarget target
public static TTarget CreateItemsPage<TTarget, TSource>(this TTarget target, IEnumerable<TSource> items, int page = 1) where TTarget : IPaginationResponse<TSource> {
...
target.Items = (chunks.ToArray())[page-1].ToArray();
target.Page = page;
target.TotalPages = totalChunks;
return target;
}
Inside controller I invoke it in the same way
return Ok(new FileModel.Response().CreateItemsPage(pages, requestData.Page));
As both Response classes are different in Item type only, a single generic type could be used instead of two non-generic types.
public class ResponsePageTemplate<TItem>
{
public TItem[] Items { get; set; }
public int Page { get; set; }
public int TotalPages { get; set; }
}
Then extension method would be like:
public static ResponsePageTemplate<TSource> CreateItemsPage<TSource>(this IEnumerable<TSource> items, int page = 1)
{
int maxItemsPerPage = 50;
var chunks = items.Select((v, i) => new {Value = v, Index = i})
.GroupBy(x => x.Index / maxItemsPerPage).Select(grp => grp.Select(x => x.Value));
int totalChunks = chunks.Count();
if (totalChunks == 0)
{
return new ResponsePageTemplate<TSource>();
}
page = page > 1 ? page : 1;
page = totalChunks < page ? 1 : page;
var result = new ResponsePageTemplate<TSource>
{
Items = (chunks.ToArray())[page - 1].ToArray(),
Page = page,
TotalPages = totalChunks
};
return result;
}
And usage would be like:
return Ok(items.CreateItemsPage(requestData.Page));

Generic Filter and Sort Using NHibernate

Is there a way to implement Generic filter for NHibernate on a repository layer? As per Microsoft API Guidelines, the sort and filters are passed as string API Guidelines. what will be the approach to that using NHibernate to make it generic?
Please have a look at this post.There is a section Paging with NHibernate.
The code snippet below is from this post.
The approach is to implement a class (in the post it was PagedResult<T>) in your case FilteredResult<T>. Having this class you can implementan extension (in post was NHibernatePagedResultExtensions) in this case NHibernateFilteredResultExtensions.
And the last thing left is to apply this extansion exacly the same way.
Hope this will help.
public abstract class PagedResultBase
{
public int CurrentPage { get; set; }
public int PageCount { get; set; }
public int PageSize { get; set; }
public int RowCount { get; set; }
public string LinkTemplate { get; set; }
public int FirstRowOnPage
{
get { return (CurrentPage - 1) * PageSize + 1; }
}
public int LastRowOnPage
{
get { return Math.Min(CurrentPage * PageSize, RowCount); }
}
}
public class PagedResult<T> : PagedResultBase
{
public IList<T> Results { get; set; }
public PagedResult()
{
Results = new List<T>();
}
}
public static class NHibernatePagedResultExtensions
{
public static async Task<PagedResult<T>> GetPagedAsync<T>(this IQueryable<T> query, int page, int pageSize)
{
var result = new PagedResult<T>
{
CurrentPage = page,
PageSize = pageSize,
RowCount = query.Count()
};
var pageCount = (double)result.RowCount / pageSize;
result.PageCount = (int)Math.Ceiling(pageCount);
var skip = (page - 1) * pageSize;
result.Results = await query.Skip(skip).Take(pageSize).ToListAsync();
return result;
}
}
public async Task<IActionResult> Index(int page = 1)
{
var result = await _session.RunInTransaction(async () =>
{
var books = await _session.Books
.Where(b => b.Title.StartsWith("How to"))
.GetPagedAsync(page, pageSize: 25);
return _mapper.Map<PagedResult<BookModel>>(books);
});
return View(result);
}

Entity IQueryable with page, search and sort

I'm sure this has been answered before, but can't seem to find it.
I'm looking for a simple download (or tutorial) for building an IQueryable that can be sorted, paged and searched.
e.g.
IQueryable<T> myThings , params[] searchKeyValuePairs, params[] orderKeyValuePairs, int pageSize, int pageNumber
Or something similar. Guessing it will be using Dynamic Linq.
My requirement is for a number of MVC3 views for broadly similar data. BA specified about 20 views of each of 10 data types...but MOST of these views are simply "All Today" and "Sorted by Cost" type views.
Data is from SQL or Oracle through EF4.
For paging:
public class PagingList<T> : List<T>
{
public int PageIndex { get; set; }
public int PageSize { get; set; }
public int TotalCount { get; set; }
public int TotalPages { get; set; }
public string pagerange { get; set; }
public int pagingrange { get; set; }
public PagingList(IQueryable<T> data, int page, int pagesize)
{
PageIndex = page;
PageSize = pagesize;
TotalCount = data.Count();
TotalPages = (int)Math.Ceiling(TotalCount /(double)PageSize);
this.AddRange(data.Skip(PageIndex * PageSize).Take(PageSize));
}
//public void GeneratePageRange()
//{
// for (int i = 1; i <= TotalPages; i++)
// {
// pagingrange = i
// }
//}
public bool HasPreviousPage
{
get { return (PageIndex > 0); }
}
public bool HasNextPage
{
get { return (PageIndex + 1 < TotalPages); }
}
}
For searching:
public static class DataYouWant
{
/// <summary>
/// Function which returns the Griddata for json result of jqGrid
/// </summary>
public static GridData Getdata<T>(ObjectSet<T> baseList,int currentPage,int rowsPerPage,
string sortcolumn,
string sortord,
string searchQuery,
string searchColumns)where T: class
{
var query = baseList.OrderBy("it." + sortcolumn + " " + sortord);
string strPredicate = string.Empty;
dynamic searchvalue = searchQuery;
if (!string.IsNullOrEmpty(searchColumns))
{
var coltype = baseList.EntitySet.ElementType.Members[searchColumns].TypeUsage.EdmType;
if (CheckIntType(coltype))
{
strPredicate = "it." + searchColumns + " = #" + searchColumns;
searchvalue = Convert.ToInt32(searchQuery);
}
else
strPredicate = "CONTAINS(it." + searchColumns + ",#" + searchColumns + ")";
query = baseList.Where(strPredicate, new ObjectParameter(searchColumns, searchvalue)).OrderBy("it." + sortcolumn + " " + sortord);
}
var pageddata = new PagingList<T>(query, currentPage, rowsPerPage);
return new GridData()
{
Page = pageddata.PageIndex + 1,
Records = pageddata.TotalCount,
Rows = pageddata,
Total = pageddata.TotalPages,
UserData = "ok"
};
}
/// <summary>
/// Checks the EdmType and
/// </summary>
public static bool CheckIntType(EdmType objEdmType)
{
switch (objEdmType.Name)
{
case "Int32":
return true;break;
case "Int16":
return true;break;
case "Int64":
return true; break;
case "Decimal":
return true;break;
default:
return false;
break;
}
}
}

How to make paging start from 1 instead of 0

I used the paging example of the Nerddinner tutorial. But I also wanted to add page Numbers, somehting like that:
<<< 1 2 3 4 5 6 >>>
The code below works if i start my paging from 0, but not from 1. How can I fix this ?
Here is my code:
PaginatedList.cs
public class PaginatedList<T> : List<T> {
public int PageIndex { get; private set; }
public int PageSize { get; private set; }
public int TotalCount { get; private set; }
public int TotalPages { get; private set; }
public PaginatedList(IQueryable<T> source, int pageIndex, int pageSize) {
PageIndex = pageIndex;
PageSize = pageSize;
TotalCount = source.Count();
TotalPages = (int) Math.Ceiling(TotalCount / (double)PageSize);
this.AddRange(source.Skip(PageIndex * PageSize).Take(PageSize));
}
public bool HasPreviousPage {
get {
return (PageIndex > 0);
}
}
public bool HasNextPage {
get {
return (PageIndex+1 < TotalPages);
}
}
}
UserController.cs
public ActionResult List(int? page)
{
const int pageSize = 20;
IUserRepository userRepository = new UserRepository();
IQueryable<User> listUsers = userRepository.GetAll();
PaginatedList<User> paginatedUsers = new PaginatedList<User>(listUsers, page ?? 0, pageSize);
return View(paginatedUsers);
}
List.cshtml
#if (Model.HasPreviousPage)
{
#Html.RouteLink(" Previous ", "PaginatedUsers", new { page = (Model.PageIndex - 1) })
}
#for (int i = 1; i <= Model.TotalPages; i++)
{
#Html.RouteLink(#i.ToString(), "PaginatedUsers", new { page = (#i ) })
}
#if (Model.HasNextPage)
{
#Html.RouteLink(" Next ", "PaginatedUsers", new { page = (Model.PageIndex + 1) })
}
PaginatedList.cs
.Skip((PageIndex -1) * PageSize).Take(PageSize)
UserController.cs
public ActionResult List(int page = 1)
{
I have made this changes :
PaginatedList.cs
public PaginatedList(IQueryable<T> source, int pageIndex, int pageSize) {
.
.
.
this.AddRange(source.Skip((PageIndex -1) * PageSize).Take(PageSize));
}
public bool HasPreviousPage {
get {
return (PageIndex > 1);
}
}
public bool HasNextPage {
get {
return (PageIndex < TotalPages);
}
}
UserController.cs
public ActionResult List(int? page)
{
const int pageSize = 20;
page = (page < 1) ? 1 : page ?? 0;
.
.
.
I would suggest to use a library instead of going to write your own pagination code. MvcPaging is one of those library.
See my answer here - what good libraries to use for paging on custom html (not table based)?

MongoDB c# : Question about pagination

Using a paged result of some query i need to get from what page is a point.The object is return the data positioned at the right page when you push the point out of the scope opening the paged result at this page.
If the paged result can be obtain like this sample , how i can get from an item from what page is comming ?
paging
.skip(PAGE_SIZE * (PAGE_NUMBER - 1)).limit(PAGE_SIZE)
public List<BsonItem> GetData(QueryComplete query, int take, int skip, SortByBuilder sort)
{
var cursor = Db.Data.FindAs<BsonItem>(query);
if (skip > 0)
cursor.SetSkip(skip);
if (take > 0)
cursor.SetLimit(take);
if (sort != null )
cursor.SetSortOrder(sort);
return cursor.ToList();
}
Thanks for your help.
Page number should( and possible sort order and direction) come from the client side. So client click on the some page and than..
Personally i using some kind of filter that contains all properties that you need.
Using that filter you just need to specify page that you need to display in grid and how much items you need per page.
var pageNumber = 1;// current page should come from the client
var filter = new BaseFilter(){CurrentPage = pageNumber, ItemsPerPage = 30};
var items = GetItemsByFilter(filter, Query.LTE("SomeDate",DateTime.Now)),
SortBy.Ascending("SortField"));
//For basic paging you only following three properties
var totalCount = filter.TotalCount; // here will be total items count
var pagesCount = filter.TotalPagesCount; // here will be total pages count
// pageNumber = current page
Also you can inferit from BasicFilter and add any properties that you need for quering, sorting.
Here filter code(hope it will be useful for you):
public List<Item> GetItemsByFilter(BaseFilter filter,
QueryComplete query, SortByBuilder sort)
{
var resultItems = new List<Item>();
var cursor = Db.Data.FindAs<BsonItem>(query);
cursor.SetSortOrder(sort);
if (filter.IsNeedPaging)
{
cursor.SetSkip(filter.Skip).SetLimit(filter.Take);
filter.TotalCount = cursor.Count();
}
resultItems.AddRange(cursor);
return resultItems;
}
public class BaseFilter
{
private int _itemsPerPage = 10;
private int _skip = 0;
private int _currentPage = 1;
public BaseFilter()
{
IsNeedPaging = true;
}
public int Skip
{
get
{
if (_skip == 0)
_skip = (CurrentPage - 1) * _itemsPerPage;
return _skip;
}
set
{
_skip = value;
}
}
public int Take
{
get
{
return _itemsPerPage;
}
set
{
_itemsPerPage = value;
}
}
public bool IsNeedPaging { get; set; }
public int TotalCount { get; set; }
public int CurrentPage
{
get
{
return _currentPage;
}
set
{
_currentPage = value;
}
}
public int ItemsPerPage
{
get
{
return _itemsPerPage;
}
set
{
_itemsPerPage = value;
}
}
public int TotalPagesCount
{
get
{
return TotalCount / ItemsPerPage +
((TotalCount % ItemsPerPage > 0) ? 1 : 0);
}
}
}

Categories