【问题标题】:MVC3 IPagedList in ViewModelViewModel 中的 MVC3 IPagedList
【发布时间】:2013-03-31 21:07:17
【问题描述】:

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

我有一个当前看起来像这样的视图模型:

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>

【问题讨论】:

  • 您是否进行了检查以确保您将数据保存到 InventoryList 中?
  • 没有数据,但应该加载一个空表。我意识到 defaultPageSize 从未在构造函数中设置。该行应该从 web.config 文件中获取 defaultPageSize。
  • page.HasValue ? page.Value - 1 : 0 在页面没有值时返回0...如果将其更改为1 的值会怎样?
  • 同样的错误,ToPagedList 的第一个参数是 pageIndex,所以如果它是 0 则无关紧要。第二个索引是 pageSize,并且因为 defaultPageSize 不是从 web 配置文件中设置的,所以pageSize 为 0。
  • 好的,如果我理解你,defaultPageSize 是问题所在,这是一个应用设置

标签: asp.net-mvc-3 pagedlist


【解决方案1】:

我想通了。我在错误的构造函数中设置了 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"]);
    }

【讨论】:

    猜你喜欢
    • 2016-10-29
    • 2013-05-31
    • 1970-01-01
    • 2012-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-04
    相关资源
    最近更新 更多