Andrew
Andrew

Reputation: 2011

MVC3 IPagedList in ViewModel

I want to have a page that has a form on the top of the page to enter movies in a system, and on the bottom of the page I want to have a table to display all of the movies in inventory. I am getting an error saying: Value cannot be less than 1. Parameter name: Page Size.

I have a viewmodel that currently looks like this:

public class InventoryViewModel
{
    public Inventory Inventory { get; set; }
    public IPagedList<Inventory> InventoryList { get; set; }
}

In my controller I have:

public ActionResult Index(int? page)
    {
        ViewBag.MoviesList = new SelectList(inventoryRepository.Movies, "MovieId", "Title");

        InventoryViewModel vm = new InventoryViewModel
        {
            Inventory = new Inventory(),
            InventoryList = inventoryRepository.GetInventory.ToPagedList(page.HasValue ? page.Value - 1 : 0, defaultPageSize)
        };
        return View(vm);
    }

In my view I have:

<div class="well">
<h4>Enter Movie in System:</h4>

@using (Html.BeginForm()) {
@Html.ValidationSummary(true, "Movie was not entered in system. Please correct the errors and try again.")
    <div>
        <div class="input-prepend">
            <span class="add-on"><i class="icon-film"></i></span>
            @Html.DropDownListFor(m => m.Inventory.MoviesId, (SelectList)ViewBag.MoviesList)
            @Html.ValidationMessageFor(m => m.Inventory)
        </div>      


        <div class="input-prepend">
            <span class="add-on"><i class="icon-calendar"></i></span>
            @Html.TextBox("Quantity")
        </div>
        <p><button class="btn btn-primary" type="submit" value="Submit">Submit</button></p>
        @Html.ValidationSummary()
    </div>
}
</div>

<div>
    <h3>Current Inventory:</h3>
</div>
<table class="table table-bordered table-hover">
    <thead>
        <tr>
            <th style="width: 15%;">Checkout Number</th>
            <th style="width: 15%;">Title</th>
            <th style="width: 23%;">Availability</th>
            <th style="width: 17%;"></th>
        </tr>
    </thead>
    <tbody>
        @foreach (var vm in Model.InventoryList.OrderBy(m => m.CheckoutNum))
        {
            <tr>
                <td>@vm.CheckoutNum</td>
                <td>@vm.Movies.Title</td>
                <td>@vm.isAvail</td>
                <td>

                </td>
            </tr>
        }
    </tbody>
    <tfoot>
       <tr>
          <td colspan="4">
                @Html.Pager(Model.InventoryList.PageSize, Model.InventoryList.PageNumber, Model.InventoryList.TotalItemCount).Options(o => o
                    .DisplayTemplate("BootstrapPagination").RouteValues(new { q = ViewBag.Query, area = "" } )
                    .AlwaysAddFirstPageNumber().MaxNrOfPages(5))
          </td>
        </tr>
    </tfoot>
</table>

Upvotes: 1

Views: 1065

Answers (1)

Andrew
Andrew

Reputation: 2011

I figured it out. I was setting the defaultPageSize in the wrong constructor, so the variable was never getting set, causing the page size to be 0.

    int defaultPageSize;
    private IInventoryRepository inventoryRepository;

    public InventoryController()
    {
        this.inventoryRepository = new InventoryRepository(new MovieContext());
        this.defaultPageSize = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["defaultPageSize"]);
    }

    public InventoryController(IInventoryRepository inventoryRepository)
    {
        this.inventoryRepository = inventoryRepository;
        this.defaultPageSize = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["defaultPageSize"]);
    }

Upvotes: 2

Related Questions