Asp.net mvc 如何使用MVC将分页列表与ViewModel一起使用
我正试图找到一种解决方案,用ViewModel创建分页列表 教程(仅使用普通型号) 类似问题 我的代码 控制器Asp.net mvc 如何使用MVC将分页列表与ViewModel一起使用,asp.net-mvc,razor,asp.net-core-mvc,Asp.net Mvc,Razor,Asp.net Core Mvc,我正试图找到一种解决方案,用ViewModel创建分页列表 教程(仅使用普通型号) 类似问题 我的代码 控制器 public async Task<IActionResult> Index(string sortOrder, string currentFilter, string searchString, int? page) { ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrde
public async Task<IActionResult> Index(string sortOrder, string currentFilter, string searchString, int? page)
{
ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
if (searchString != null)
{
page = 1;
}
else
{
searchString = currentFilter;
}
ViewData["CurrentFilter"] = searchString;
var palletAccounts = from p in _context.PalletAccount
select p;
if (!String.IsNullOrEmpty(searchString))
{
palletAccounts = palletAccounts.Where(s => s.AccountName.Contains(searchString));
}
switch (sortOrder)
{
case "name_desc":
palletAccounts = palletAccounts.OrderByDescending(s => s.AccountName);
break;
default:
palletAccounts = palletAccounts.OrderBy(s => s.AccountName);
break;
}
var palletAccountSelectListItems = (from p in _context.PalletAccount
select p.PalletGroupName).Distinct().Select(a => new SelectListItem(a,a));
int pageSize = 10;
return View(new PalletAccountViewModel { PalletAccounts = await PaginatedList<PalletAccount>.CreateAsync(palletAccounts.AsNoTracking(), page ?? 1, pageSize), GroupNames = await palletAccountSelectListItems.ToListAsync()});
}
@model PaginatedList<PalletPortal.Models.ViewModels.PalletAccountViewModel> <--if I use the model without PaginatedList it works fine (except the paging obviously).
@{
ViewData["Title"] = "Pallkonto";
}
<div class="space"></div>
<section class="main-section">
<table class="table" id="tableCenter">
<thead>
<tr>
<th>
<a asp-action="Index" asp-route-sortOrder="@ViewData["NameSortParm"]" asp-route-currentFilter="@ViewData["CurrentFilter"]">@Html.DisplayNameFor(model => model.PalletAccount.AccountName)</a>
</th>
<th>
@Html.DisplayNameFor(model => model.PalletAccount.PalletGroupName)
</th>
<th>
@Html.DisplayNameFor(model => model.PalletAccount.PalletAccountType)
</th>
<th>
@Html.DisplayNameFor(model => model.PalletAccount.IsActive)
</th>
<th> <button class="button" id="myBtn"><span>Skapa pallkonto </span></button></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.PalletAccounts)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.AccountName)
</td>
<td>
@Html.DisplayFor(modelitem => item.PalletGroupName)
</td>
<td>
@Html.DisplayFor(modelitem => item.PalletAccountType)
</td>
<td>
@Html.DisplayFor(modelItem => item.IsActive, new { @class = "checkbox" })
</td>
<td>
<a asp-action="Delete" asp-route-id="@item.ID"><button class="button" style="vertical-align: middle"><span>Radera </span></button></a>
<a asp-action="Edit" asp-route-id="@item.ID"><button class="button" style="vertical-align: middle"><span>Redigera </span></button></a>
</td>
</tr>
}
</tbody>
</table>
@{
var prevDisabled = !Model.HasPreviousPage ? "disabled" : "";
var nextDisabled = !Model.HasNextPage ? "disabled" : "";
}
<a asp-action="Index"
asp-route-sortOrder="@ViewData["CurrentSort"]"
asp-route-page="@(Model.PageIndex - 1)"
asp-route-currentFilter="@ViewData["CurrentFilter"]"
class="btn btn-default @prevDisabled">
Previous
</a>
<a asp-action="Index"
asp-route-sortOrder="@ViewData["CurrentSort"]"
asp-route-page="@(Model.PageIndex + 1)"
asp-route-currentFilter="@ViewData["CurrentFilter"]"
class="btn btn-default @nextDisabled">
Next
</a>
public异步任务索引(string sortOrder、string currentFilter、string searchString、int?页)
{
ViewBag.NameSortParm=String.IsNullOrEmpty(排序器)?“名称描述”:“;
if(searchString!=null)
{
page=1;
}
其他的
{
searchString=currentFilter;
}
ViewData[“CurrentFilter”]=searchString;
var palletAccounts=来自_context.PalletAccount中的p
选择p;
如果(!String.IsNullOrEmpty(searchString))
{
palletAccounts=palletAccounts.Where(s=>s.AccountName.Contains(searchString));
}
开关(分拣机)
{
案例“名称描述”:
palletAccounts=palletAccounts.OrderByDescending(s=>s.AccountName);
打破
违约:
palletAccounts=palletAccounts.OrderBy(s=>s.AccountName);
打破
}
var palletAccountSelectListItems=(来自_context.PalletAccount中的p
选择p.PalletGroupName).Distinct()。选择(a=>new SelectListItem(a,a));
int pageSize=10;
返回视图(新PalletAccountViewModel{PalletAccounts=await PaginatedList.CreateAync(PalletAccounts.AsNoTracking(),第1页,pageSize),GroupName=await palletAccountSelectListItems.ToListSync());
}
模型视图
public class PalletAccountViewModel
{
public PalletAccount PalletAccount { get; set; }
public IEnumerable<PalletAccount> PalletAccounts { get; set; }
public IEnumerable<SelectListItem> GroupNames { get; set; }
}
public类PalletAccountViewModel
{
公共托盘计数托盘计数{get;set;}
公共IEnumerable PalletAccounts{get;set;}
公共IEnumerable组名{get;set;}
}
索引页(剃须刀)
@model PaginatedList model.PalletAccount.AccountName)
@DisplayNameFor(model=>model.PalletAccount.PalletGroupName)
@DisplayNameFor(model=>model.PalletAccount.PalletAccountType)
@DisplayNameFor(model=>model.PalletAccount.IsActive)
斯卡帕·帕尔科托
@foreach(模型中的var项目。PalletAccounts)
{
@DisplayFor(modelItem=>item.AccountName)
@DisplayFor(modelitem=>item.PalletGroupName)
@DisplayFor(modelitem=>item.PalletAccountType)
@DisplayFor(modelItem=>item.IsActive,新的{@class=“checkbox”})
拉德拉
雷迪格拉
}
@{
var prevDisabled=!Model.HasPreviousPage?“已禁用”:“;
var nextisabled=!Model.HasNextPage?“已禁用”:“;
}
以前的
下一个
我无法在索引页(PalletAccount和palletAccounts)中访问我的模型
例如:model=>model.PalletAccount.AccountName
有人能给我指出正确的方向吗
提前感谢。您的控制器正在将
PalletAccountViewModel的实例传递给视图,因此您需要将视图中的模型更改为
@model PalletAccountViewModel
然后,由于需要对PalletAccounts
属性进行分页,因此需要将视图模型更改为
public class PalletAccountViewModel
{
public PalletAccount PalletAccount { get; set; }
public PaginatedList<PalletAccount> PalletAccounts { get; set; } // change
public IEnumerable<SelectListItem> GroupNames { get; set; }
}
您视图中的模型需要是@model PalletAccountViewModel
,因为这是方法返回的,属性应该是public PaginatedList PalletAccounts{get;set;}
哦,有那么简单吗。非常感谢你!您可以回答该问题,以便我将其标记为答案。此外,您的视图模型应该包含CurrentSort
和CurrentFilter
的属性,而不是使用ViewData
@{
var prevDisabled = !Model.PalletAccounts.HasPreviousPage ? "disabled" : "";
var nextDisabled = !Model.PalletAccounts.HasNextPage ? "disabled" : "";
}