ViewModel中的MVC3 IPagedList

时间:2013-03-31 21:07:18

标签: asp.net-mvc-3 pagedlist

我希望页面顶部有一个表单可以在系统中输入电影,而在页面底部我想要一个表格来显示库存中的所有电影。我收到一个错误说:值不能小于1.参数名称:页面大小。

我有一个目前看起来像这样的viewmodel:

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

在我的控制器中我有:

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);
    }

在我看来,我有:

<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>

1 个答案:

答案 0 :(得分:2)

我明白了。我在错误的构造函数中设置defaultPageSize,因此变量永远不会被设置,导致页面大小为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"]);
    }