How to pass information from controller to viewdata? asp net mvc c#
I am building a pagination my mvc project and have follew problem. I did everything for pagination and now need just pass a page information to view data in view.
I have a user control Pagination:
Pagination.ascx:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Pagination.ascx.cs"
Inherits="PIMP.Web.TestForum.Views.Shared.Pagination" %>
<ul id="pagination-flickr">
<% if (ViewData.Model.HasPreviousPage)
{ %>
<li class="previous"><a href="<%=ViewData.PageActionLink.Replace("%7Bpage%7D", (ViewData.PageIndex - 1).ToString())%>">
« Previous</a></li>
<% }
else
{ %>
<li class="previous-off">« Previous</li>
<% } %>
<%for (int page = 1; page <= ViewData.Model.TotalPages; page++)
{
if (page == ViewData.Model.PageIndex)
{ %>
<li class="active">
<%=page.ToString()%></li>
<% }
else
{ %>
<li><a href="<%=ViewData.PageActionLink.Replace("%7Bpage%7D", page.ToString())%>">
<%=page.ToString()%></a></li>
<% }
}
if (ViewData.Model.HasNextPage)
{ %>
<li class="next"><a href="<%=ViewData.PageActionLink.Replace("%7Bpage%7D", (ViewData.PageIndex + 1).ToString())%>">
Next »</a></li>
<% }
else
{ %>
<li class="next-off">Next »</li>
<% } %>
</ul>
<ul id="pagination-flickr0">
<li><a href="<%=ViewData.PageActionLink.Replace("%7Bpage%7D", page.ToString())%>"></a></li>
</ul>
Pagination.ascx.cs:
public partial class PaginationViewData
{
public int PageIndex { get; set; }
public int TotalPages { get; set; }
public开发者_StackOverflow中文版 int PageSize { get; set; }
public int TotalCount { get; set; }
public string PageActionLink { get; set; }
public bool HasPreviousPage
{
get
{
return (PageIndex > 1);
}
}
public bool HasNextPage
{
get
{
return (PageIndex * PageSize) <= TotalCount;
}
}
}
public partial class Pagination : System.Web.Mvc.ViewUserControl<PaginationViewData>
{
public Pagination()
{
}
}
class PagedList.cs:
namespace PIMP.Web.TestForum.DataObject.Forum
{
public class PagedList<T>: List<T>
{
public PagedList(IQueryable<T> source, int index, int pageSize)
{
this.TotalCount = source.Count();
this.PageSize = pageSize;
this.PageIndex = index;
this.AddRange(source.Skip((index - 1) * pageSize).Take(pageSize).ToList());
int pageResult = 0;
for (int counter = 1; pageResult < this.TotalCount; counter++)
{
pageResult = counter * this.PageSize;
this.TotalPages = counter;
}
}
public PagedList()
{
}
public int TotalPages
{
get;
set;
}
public int TotalCount
{
get;
set;
}
public int PageIndex
{
get;
set;
}
public int PageSize
{
get;
set;
}
public bool HasPreviousPage
{
get
{
return (PageIndex > 1);
}
}
public bool HasNextPage
{
get
{
return (PageIndex * PageSize) <= TotalCount;
}
}
}
public static class Pagination
{
public static PagedList<T> ToPagedList<T>(this IQueryable<T> source, int index, int pageSize)
{
return new PagedList<T>(source, index, pageSize);
}
public static PagedList<T> ToPagedList<T>(this IQueryable<T> source, int index)
{
return new PagedList<T>(source, index, 10);
}
}
}
in View I am doing following:
<% Html.RenderPartial("Pagination", new NISE.Web.TestForum.Views.Shared.PaginationViewData()
{
PageIndex = ViewData.Model.PageIndex,
TotalPages = ViewData.Model.TotalPages,
PageActionLink = Url.Action("Forum", "Thread", new { id = this.Model.Id, page = "{page}" }),
TotalCount = ViewData.Model.TotalCount,
PageSize = ViewData.Model.PageSize
}, null);%>
I dont know what I should do in my controller. How can I pass Page Information to the ViewData? My controller looks like that, can you help me to extend it??
[HttpGet]
[ValidateInput(false)]
public ActionResult Thread(Guid id)
{
try
{
ThreadModel model = new ThreadModel(id);
model.IncreaseHitCount();
PagedList<ListView> list = new PagedList<ListView>();
list.PageIndex = 0;
list.PageSize = 0;
list.TotalCount = 0;
list.TotalPages = 0;
return View(model);
}
catch (Exception ex)
{
bool rethrow = ExceptionHelper.Handle(ex, "Business Logic");
if (rethrow)
{
throw;
}
}
return View();
}
Well, I did it like this:
I created a "view-model" of (my custom) data grid which contains infos about the current page, page size, etc.
it looks like this:
using System.Collections.Generic;
public class GridContent<T>
{
public IEnumerable<T> Data { get; set; }
public int PageIndex { get; set; }
public int TotalPages { get; set; }
public int TotalItems { get; set; }
}
in the controller I then return the following:
return View(new GridContent<Entity>()
{
Data = entityEnumeration, // your actual data
PageIndex = pageIndex,
TotalItems = totalItems,
TotalPages = totalItems / pageSize + 1
});
and finally on the view
<%@ Page Title="SomeTitle" Language="C#" Inherits="System.Web.Mvc.ViewPage<GridContent<Entity>>" %>
Now you can access the pagination information as well as the data using the wrapper model / class GridContent.
Hope this helps.
精彩评论