Using a Pagedlist with a Viewmodel ASP.NET MVC

Using a PagedList with a ViewModel ASP.Net MVC

I modified the code as follow:

ViewModel

using System.Collections.Generic;
using ContosoUniversity.Models;

namespace ContosoUniversity.ViewModels
{
public class InstructorIndexData
{
public PagedList.IPagedList<Instructor> Instructors { get; set; }
public PagedList.IPagedList<Course> Courses { get; set; }
public PagedList.IPagedList<Enrollment> Enrollments { get; set; }
}
}

Controller

public ActionResult Index(int? id, int? courseID,int? InstructorPage,int? CoursePage,int? EnrollmentPage)
{
int instructPageNumber = (InstructorPage?? 1);
int CoursePageNumber = (CoursePage?? 1);
int EnrollmentPageNumber = (EnrollmentPage?? 1);
var viewModel = new InstructorIndexData();
viewModel.Instructors = db.Instructors
.Include(i => i.OfficeAssignment)
.Include(i => i.Courses.Select(c => c.Department))
.OrderBy(i => i.LastName).ToPagedList(instructPageNumber,5);

if (id != null)
{
ViewBag.InstructorID = id.Value;
viewModel.Courses = viewModel.Instructors.Where(
i => i.ID == id.Value).Single().Courses.ToPagedList(CoursePageNumber,5);
}

if (courseID != null)
{
ViewBag.CourseID = courseID.Value;
viewModel.Enrollments = viewModel.Courses.Where(
x => x.CourseID == courseID).Single().Enrollments.ToPagedList(EnrollmentPageNumber,5);
}

return View(viewModel);
}

View

<div>
Page @(Model.Instructors.PageCount < Model.Instructors.PageNumber ? 0 : Model.Instructors.PageNumber) of @Model.Instructors.PageCount

@Html.PagedListPager(Model.Instructors, page => Url.Action("Index", new {InstructorPage=page}))

</div>

I hope this would help you!!

How to return ViewModel PagedList [ASP MVC]

Update your view model to use the Paged list

public class CategoryVm {
public IPagedList<Book> Book { set; get; }

public Category Category_Content { get; set; }
}

Then assign the paged list to your view model

public ActionResult Category(int? id, int? page) {
int pageSize = 6;
int pageNumber = page ?? 1;
if (id != null) {
var vm = new CategoryVm();
vm.Category_Content = db.Categories.Where(t => t.Category_id == id).Single();
var books = db.Books.Where(b => b.Category_id == id).ToList();
if (vm != null) {
//Apply paged list to view model.
vm.Books = books.ToPagedList(pageNumber, pageSize);
return View(vm);
}
return HttpNotFound();
}

return RedirectToAction("index",new { Controller = "Home" });
}

Now in your view change the model.

@model SmartBookLibrary.ViewModel.CategoryVm

And

Page @(Model.Book.PageCount < Model.Book.PageNumber ? 0 : Model.Book.PageNumber) of @Model.Book.PageCount
@Html.PagedListPager(Model.Book, page => Url.Action("Category", new {page}))

ASP.NET MVC Pagination Not Working on ViewModel

ToPagedList() is designed to work with data models, not view models. What you are currently doing is returns all News and Accouncement records from the database by calling .ToList() (which defeats the whole purpose of using server side paging), and the assigning all those records to the property of one instance of your view model, and then applying ToPagedList() to that single instance.

You need to change your view model properties to IPagedList<T> and then apply paging to each property, not the view model itself. Your view model should be

public class SearchViewModel
{
public string SearchText { get; set; }
public IPagedList<News> News { get; set; }
public IPagedList<Announcement> Annoucements { get; set; }
}

and then the controller method will be (note that your linq queries need to include an orderby clause, but I do not know which property you want to order by)

public ActionResult Search(string searchText, int? page)
{
int pageIndex = page ?? 1;
int dataCount = 20;

dbContext=new dbContext();
IQueryable<News> news = (from news in dbContext.News
where news.Title.Contains(searchText) orderby ?? select news);
IQueryable<News> announcements = (from announcement in dbContext.Announcement
where announcement.Title.Contains(searchText) orderby ?? select announcement);

SearchViewModel model = new SearchViewModel
{
SearchText = searchText,
News = news.ToPagedList(pageIndex, dataCount)
Annoucements = announcements.ToPagedList(pageIndex, dataCount)
};
return View(model);
}

Then modify the view to

@model SearchViewModel
@using PagedList
@using PagedList.Mvc

... // your form with the input for SearchText

<div class=container>
@foreach (var news in Model.News)
{
@news.Title
}
@foreach(var announcement in Model.Announcements)
{
@announcement.Title
}
@if (Model.News.Count() > Model.Announcements.Count())
{
@Html.PagedListPager(Model.News, page=>Url.Action("Search",new { page = page, searchText = Model.SearchText }));
}
else
{
@Html.PagedListPager(Model.Announcements, page=>Url.Action("Search",new { page = page, searchText = Model.SearchText }));
}
</div>

Note that the if block ensures that the page numbers are shown in the case where you navigate to subsequent pages where you might have some News items but no Announcement items or vice versa. However this does not guarantee that the total number of pages will be correct (there might be say 45 matching News records, and 65 matching Announcement records)

PagedList a List of objects in a ViewModel

Try something like this:

public class ProductsPageViewModel() 
{
public PagedList.IPagedList<MyProject.ViewModel.AddProductsViewModel> AddProductsPagedList { get; set; }
// other properties/methods for your view go here...
}

Then in your view:

@model ProductsPageViewModel

// other HTML here....

Página: @(Model.AddProductsPagedList.PageCount < Model.AddProductsPagedList.PageNumber ? 0 : Model.AddProductsPagedList.PageNumber) de: @Model.AddProductsPagedList.PageCount
@Html.PagedListPager(Model.AddProductsPagedList, page => Url.Action("Index", new
{
page,
sortOrder =
ViewBag.CurrentSort,
currentFilter = ViewBag.CurrentFilter
}))

How to use IPagedList paging feature with viewmodel and join in asp .net mvc

Got my Answer:

var po = from s in db.Sponsers
join st in db.SponserTypes on s.SponserTypeId equals st.Id into st2
from st in st2.DefaultIfEmpty
orderby st.DisplayOrder
select new SponserDisplayViewModel { Sponser = s, SponserType = st };
return View(po.ToPagedList(pageIndex, pageSize));

How to implement pagedlist in mvc when using view models?

If you need to map your entities to view models and still be able to page through them, then you need to use StaticPagedList. In order for the standard method of using PagedList to work, it needs to be passed a queryable (unevaluated), so that it can limit the query to only pull the proper number of records. If you're mapping to a view model, though, then that's going to evaluate the query.

What you need is something like:

public ActionResult Students(int? page)
{
var pageNumber = page ?? 1;
var pageSize = 10;

var totalStudents = db.Students.Count();
var students = db.Students.Skip((pageNumber - 1) * pageSize).Take(pageSize);

var model = // map `students` to your view model

var pagedList = new StaticPagedList<MyViewModel>(model, pageNumber, pageSize, totalStudents);

return View(pagedList);
}

Then, the model for your view is:

@model PagedList.IPagedList<Namespace.To.MyViewModel>


Related Topics



Leave a reply



Submit