Search code examples
c#asp.net-mvc-3viewmodelpagedlist

PagedList error when passing Data with Viewmodel


Hi I was wondering if anyone con shed some light with a problem that I´m experiencing when implementing pagedlist code (https://github.com/TroyGoode/PagedList) in my asp.net mvc3 website. This are the details of what I´m trying to do:

I created a Viemodel with this details:

public class ProductViewModelList
{
    public List<Product> ProductBrowse { get; set; }
    public int NumberOfProducts { get; set; } 
    public List<Category> CategoryModel { get; set; } 
}

Then a Controller holding the information that I want to pass to the view:

public ActionResult List(int categoryid, int? page)
{
    const int defaultPageSize = 20;
    int currentPageIndex = page.HasValue ? page.Value - 1 : 0;
    var categoryModel = db.Category.Include("Product").Single(c => c.CategoryId == categoryid);
    var paginatedmodel = categoryModel.Product.ToPagedList(currentPageIndex, defaultPageSize);
    var viewModel = new ProductViewModelList
    {
        ProductBrowse = paginatedmodel.ToList(),
        NumberOfProducts = categoryModel.Product.Count()
    };
return View(viewModel);

Finally the View that starts with:

@inherits System.Web.Mvc.WebViewPage<IPagedList<Social.ViewModels.ProductViewModelList>>
@using Social.Helpers;
@using System.Web.Mvc.Html

and create a foreach with pager in this way:

@foreach (var item in Model)
                {
                    <div class="result">
<div class="info_result"><h2><a>@Html.ActionLink(Html.TruncateText(item.Title, 25), "Details", new { id = item.ProductId })</a></h2><p><a>@Html.ActionLink(Html.TruncateText(item.Description, 180), "Details", new { id = item.ProductId })</a></p<a>@String.Format("{0:dddd, MMMM d, yyyy}", item.CreatedOn)</a></div>

<div class="paginacion">@Html.Pager(Model.PageSize, Model.PageNumber, Model.TotalItemCount)</div>

Information: 1- Im using

@inherits System.Web.Mvc.WebViewPage<IPagedList<Social.ViewModels.ProductViewModelList>>

because I need to pass information from the IPagedList and from the ProductViewModelList to the View.

2- If I pass inherits like <IPagedList<Social.ViewModels.ProductViewModelList> I received complete intellisense for IPagedList properties but for Viewmodels NumberofProducts, ProductBrowse, CategoryModel I only receive their names but not ther properties like ProductBrowse.ProductId as an example.

3- I would like to display a URL of the type: http://www.domain.com/Controller/List?categoryId=2&page=1

4- I dont know what I have to do in order to include object values for

Html.Pager(Model.PageSize, Model.PageNumber, Model.TotalItemCount, ObjectValues for categoryId=2)

Sorry if this is a mess to understand, I tried my best in order to explain. thanks in advance.


Solution

  • Instead of:

    @inherits System.Web.Mvc.WebViewPage<IPagedList<Social.ViewModels.ProductViewModelList>>
    

    try:

    @model IPagedList<Social.ViewModels.ProductViewModelList>
    

    Also instead of writing foreach loops in your views you could use a display template. So in your view:

    <div class="result">
        @Html.DisplayForModel()
    </div>
    <div class="paginacion">
        @Html.Pager(Model.PageSize, Model.PageNumber, Model.TotalItemCount)
    </div>
    

    and inside ~/Views/Home/DisplayTemplates/ProductViewModelList.cshtml

    @model Social.ViewModels.ProductViewModelList
    <div class="info_result">
        <h2>
            @Html.ActionLink(
                Html.TruncateText(Model.Title, 25), 
                "Details", 
                new { id = Model.ProductId }
            )
        </h2>
        <p>  
            @Html.ActionLink(
                Html.TruncateText(Model.Description, 180), 
                "Details", 
                new { id = item.ProductId }
            )
        </p>
        <!-- I've modified this to use a display template instead of String.Format
        Now you only need to decorate your view model property with the DisplayFormat attribute like this:
        [DisplayFormat(DataFormatString = "{0:dddd, MMMM d, yyyy}")]
        public DateTime? CreatedOn { get; set; }
        -->
        @Html.DisplayFor(x => x.CreatedOn)
    </div>