【问题标题】:How could I fetch a parameter from this MVC architecture?我如何从这个 MVC 架构中获取参数?
【发布时间】:2019-04-29 12:25:03
【问题描述】:

我在一个 ASP .NET 项目中有这个 HTML 页面,我需要为一个元素表生成一个分页。问题是,在已经存在的类架构中,我的模型唯一可以拥有的是视图模型的 IEnumerable。我唯一需要的是从我的控制器或它返回的视图中获取一个整数值。从那个整数中,这将表示生成分页所需的按钮数,我将创建它,请参阅 HTML。

我的控制器生成项目列表并通过执行 SQL 请求在模型中返回它,该请求根据我的 URL 参数从某个偏移量中选择一定数量的项目。

这是 HTML,以及控制器中的代码隐藏如下:

@model  IEnumerable<ItemIndexViewModel>

<h2>@UiText.PageTitles.ITEM_LIST</h2>
<hr />
<div class="col-md-12">
    <div class="col-md-9">
        <table class="table" id="client-index">
            <thead>
                <tr>
                    <th class="green-table-head-1">
                        @Html.DisplayNameFor(model => model.Name)
                    </th>
                    <th class="green-table-head-1">
                        @Html.DisplayNameFor(model => model.Id)
                    </th>
                </tr>
            </thead>
            <tbody>
                @foreach (ItemViewModel item in Model)
                {
                    @*Here, I have my table of items being generated*@
                }
            </tbody>
        </table>
        <div id="pagination">
            <ul>
                @for (int i = 0; i < [I need my int right here]; i++)
                {
                    @*I will generate buttons here*@
                }
            </ul>
        </div>
    </div>
</div>

public virtual ActionResult Index()
{
    int ownerId = _httpContext.GetUserOwnerId();
    int amountPerPage = 0;
    int pageIndex = 0;

    Int32.TryParse(Request.QueryString["amountPerPage"], out amountPerPage);
    Int32.TryParse(Request.QueryString["pageIndex"], out pageIndex);

    if (amountPerPage <= 0)
    {
        amountPerPage = 10;
    }
    if (pageIndex <= 0)
    {
        pageIndex = 1;
    }

    List<Item> items = _itemRepository.GetByPage(pageIndex, amountPerPage).ToList();

    // Make view models from the list of items
    List<ItemIndexViewModel> itemIndexViewModels = Mapper.Map<List<ItemIndexViewModel>>(items);

    // Create the buttons for the HTML
    int totalAmount = _itemRepository.Count();
    int totalPages = (Int32)Math.Ceiling(Decimal.Divide(totalAmount, amountPerPage));

    // Set update the navigation trace
    SetTraceRoot(MVC.Item.Index(), MVC.Item.ActionNames.Index);

    return View(itemIndexViewModels.OrderBy(x => x.Name));
}

生成分页的好方法是什么?我正在寻找灵活性,因为此过程将针对不止一页和不止一类项目实施。我已经尝试了一些无济于事的事情,比如使用一个类来包含我的视图模型列表和一个整数来表示存储它们所需的页面数。

【问题讨论】:

  • 由于不能改变页面模型,所以使用ViewBag来传递分页对象。
  • 这正是我们所需要的。请将此作为答案发布,以便我批准。

标签: c# html asp.net asp.net-mvc-4 pagination


【解决方案1】:

您可以在控制器操作中使用ViewBagViewData 实例在返回视图之前提供分页值:

ViewBag.TotalPages = totalPages;

并将其值传递给for循环以在视图侧生成分页:

@for (int i = 0; i < ViewBag.TotalPages; i++)
{
    @* generate paging buttons *@
}

通常ViewBag 属性不需要对简单类型(数字和字符串值)进行类型转换,但请确保分配值以避免返回 null。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-28
    • 2018-12-11
    相关资源
    最近更新 更多