I've implemented pagination using PagedList.Mvc in my MVC project, By default it takes 2 parameters (page , pageSize). In my code I have made pageSize=9 which shows 9 items per page.

I want to add a dropdown with pageSize(5,10,15,20). User can select value from dropdown for filtering the record. If he selects 10 then I want to show 10 items per page.


public ActionResult Products(int page = 1, int pageSize = 9)
        List<Product> listProducts = db.Products.ToList();
        PagedList<Product> model = new PagedList<Product>(listProducts, page, pageSize);
        return View(model);


@model PagedList.IPagedList<FypStore.Models.Product>
@using PagedList.Mvc
<h2 class="title text-center">Products</h2>
        foreach (var product in Model)
            <div class="col-sm-4">
                <div class="product-image-wrapper">
                    <div class="single-products">
                        <div class="productinfo text-center">
                            <img src="~/ProductImages/@product.ImagePath" alt="@product.Name" width="250" height="300" />
                            <h2>PKR @Html.ValueFor(modelItem => product.Price)</h2>
                            <p>@Html.ActionLink(@product.Name, "Details", new { id = product.ProductId })</p>
                            @Html.ActionLink(" Add to cart", "AddToCart", "ShoppingCart", new { id = product.ProductId }, htmlAttributes: new { @class = "fa fa-shopping-cart fa-2x btn btn-default" })
<br />

@Html.PagedListPager(Model, page => Url.Action("Products", new { page, pageSize = Model.PageSize }))
Showing @Model.FirstItemOnPage to @Model.LastItemOnPage of @Model.TotalItemCount Products

I've added this code

List<string>selectPageSize = new List<string>();
SelectList sps = new SelectList(selectPageSize);
ViewBag.selectPageSize= sps;

@using (Html.BeginForm("Products", "Home", FormMethod.Post))

    @Html.DropDownList("selectPageSize", "-select pag size-")
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" class="btn btn-default" value="Filter" />

and added this line in my controller

 pageSize = Convert.ToInt32(Request.Form["selectPageSize"]);

and the error I get is:

PageSize cannot be less than 1. Parameter name: pageSize Actual value was 0.

Related posts

Recent Viewed