【发布时间】:2015-04-30 20:37:34
【问题描述】:
我正在尝试将分页添加到我的视图并编辑我的控制器来创建它,但是当我尝试包含 PagedList 模型时我收到一个错误,因为我无法包含我的原始模型。这是我现在的视图:
@model IEnumerable<metric_tool.Models.Metric>
@model PagedList.IPagedList<metric_tool.Models.Metric>
@using PagedList
@{
ViewBag.Title = "Metrics List";
}
<h2>Metrics</h2>
<p>
@Html.ActionLink("Create New", "Create")
@using (Html.BeginForm("Index", "Metric", FormMethod.Get))
{
<p>
Category: @Html.DropDownList("metricCategory","All")
Client: @Html.DropDownList("metricClient","All")
Phase: @Html.DropDownList("metricPhase","All")
Hierarchy: @Html.DropDownList("metricHierarchy", "All")
Thresholds: @Html.DropDownList("metricThreshold", "All") <br />
</p>
<p>
Data Source Search: @Html.TextBox("searchDataSrc") <input type="submit" value="Search" />
Description Search: @Html.TextBox("SearchString", ViewBag.CurrentFilter as string) <input type="submit" value="Search" />
</p>
}
</p>
<table class="table">
<tr>
<th>
@Html.ActionLink("Metric","Index", new { sortOrder = ViewBag.MetricSort, currentFilter=ViewBag.CurrentFilter })
</th>
<th>
@Html.ActionLink("Category", "Index", new { sortOrder = ViewBag.CategorySort, currentFilter = ViewBag.CurrentFilter })
</th>
<th>
@Html.DisplayNameFor(model => model.SelectClient)
</th>
<th>
@Html.DisplayNameFor(model => model.SelectPhase)
</th>
<th>
@Html.DisplayNameFor(model => model.SelectHierarchy)
</th>
<th>
@Html.DisplayNameFor(model => model.SelectDataSource)
</th>
<th>
@Html.DisplayNameFor(model => model.SelectThresholds)
</th>
<th>
@Html.DisplayNameFor(model => model.Description)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
.....
</table>
<br />
Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount
@Html.PagedListPager(Model, page => Url.Action("Index",
new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }))
我正在考虑使用动态模型,但无法让它工作。
【问题讨论】:
-
为什么两个都需要,尤其是这个:
@model IEnumerable<metric_tool.Models.Metric>这个应该够了`@model PagedList.IPagedList` -
如果我需要多个模型,我通常会用我需要的模型构建一个 ViewModel。另外,如果它适合您,请查看一些处理分页等的插件。datatables.net
-
使用 ViewModel 将两个模型封装成一个。我想给你看代码 sn-p 但不知道为什么我能回答这个问题
标签: asp.net-mvc model-view-controller model asp.net-mvc-5