7 thoughts on “MVC Pager update

  • Pingback:Tweets die vermelden Martijn Boland » MVC Pager update -- Topsy.com

  • February 13, 2011 at 5:47 pm
    Permalink

    Dear Martijn,
    thank you so much for your mvcpager. It was very important for my website development. I would like to ask you what I have to do if instead of categoryName using string I need to call categoryId from db.categories?. I cannot move forward with that receiving error “this dictionary requires a model item of type IPagedList`1[Social.ViewModels.ProductViewModelList]”
    thank you so much for your time.

  • February 14, 2011 at 10:02 am
    Permalink

    @sebastian: in your situation I’d create a view model for the page that has a SelectList of categories in it and a also an IPagedList with the results. The categories are displayed as

    <%= Html.DropDownList("CategoryId", Model.Categories) %>

    and the pager would be something like

    <%= Html.Pager(Model.Results.PageSize, Model.Results.PageNumber, Model.Results.TotalItemCount, new { CategoryId = Model.Categories.SelectedValue } )%>

  • February 14, 2011 at 4:06 pm
    Permalink

    Hi Martijn, thank you so much for your prompt response. I created the following controller taking into consideration your example

    public ActionResult Browse(int categoryId, int? page)
    {
    const int defaultPageSize = 20;
    IList allProducts = new List();
    categoryName = ?

    int currentPageIndex = page.HasValue ? page.Value – 1 : 0;
    var products = allProducts.Where(p => p.CategoryId.Equals(categoryId)).ToPagedList(currentPageIndex, defaultPageSize);

    ViewData[“CategoryDisplayName”] = categoryId;
    return View(products);
    }

    but on categorydisplayname you took the value from categoryname which is a string or ?? this.categories . In the case that you need to change that value for a category model, retrieving and integer what do you suggest to do?. sorry if its a newbie question but I´m learning with your example.
    thank you so much,brgds

  • February 20, 2011 at 6:31 am
    Permalink

    Hi Martijn, I´ve been trying but I cannot move forward in my implementation of the pager. I´m only trying to display a list of products (from db.product) filtered by categoryid (from db.category). if categoryid=2 display those products paginated. I tried to use the example with string details but with no success. I dont know what i have to do in order to use the mvc pager sucessfully. Thanks in advance for any help.
    brgds.

  • February 20, 2011 at 10:35 pm
    Permalink

    @sebastian: could you please try the solution that I proposed earlier?

    You need to create a class (ViewModel) that contains both the list of categories (as SelectList) and the results as IPagedList.
    You might also add the selected categoryId as property of the view model if the SelectedValue doesn’t work. Hopefully this will help you a little.

  • February 20, 2011 at 10:42 pm
    Permalink

    ok, I will do that. thank you!

Leave a Reply

Your email address will not be published. Required fields are marked *