【发布时间】:2019-11-15 20:04:33
【问题描述】:
我想在 Asp.net Core 2.2 中使用带有 ViewModel 的分页。
你可以在下面看到我的代码
公共类 UserQuestionListComplexViewModel
{
//这个类中有2个ViewModel
公共 UserPanelViewModel Model1 { 获取;放; }
公共列表 Model2 { get;放; }
}
在我的控制器中
公共类 UserHomeController : 控制器
{
私有只读 UserManager _userManager;
私有只读 IQuestionRepository _iq;
公共 UserHomeController(UserManager userManager,
IQuestionRepository iq)
{
_userManager = 用户管理器;
_iq = iq;
}
[HttpGet]
公共异步任务 QuestionList(UserQuestionListComplexViewModel 模型,
整数页 = 1)
{
var query = _iq.UserQuestionList(_userManager.GetUserId(HttpContext.User), page);
model.UQVM = 等待查询;
返回视图(模型);
}
}
下面是QuestionRepository
公共异步任务> UserQuestionList(string UserID, 整数页 = 1) { var questionQuery =(来自 _db.QuestionTbl 中的 q 其中 q.UserID == UserID 选择新的 UserQuestionListViewModel() { …… }) .AsNoTracking() .Where(q => q.qflag == 0) .OrderBy(q => q.QuestionID); var pagedResult = 等待 PagingList
.CreateAsync( questionQuery, 1, page); 返回分页结果; }
最后View.cshtml
@model UserQuestionListComplexViewModel
@using ReflectionIT.Mvc.Paging
@await Component.InvokeAsync("UserInfo", Model.Model1)
<div>
<table>
<thead class="thead-dark">
<tr>
<td>...</td>
</tr>
</thead>
<tbody>
@foreach (var item in Model.Model2)
{
<tr>
<td>...</td>
</tr>
}
</tbody>
</table>
<nav class="pagenav">
@await this.Component.InvokeAsync("Pager", new { PagingList = this.Model })
</nav>
</div>
但我得到以下错误
InvalidOperationException:传递到 ViewDataDictionary 的模型项的类型为“ReflectionIT.Mvc.Paging.PagingList`1[porseman.Models.ViewModels.UserQuestionListViewModel]”,但此 ViewDataDictionary 实例需要“porseman.Areas”类型的模型项.UserPanel.Models.UserComplexViewModel.UserQuestionListComplexViewModel'。
【问题讨论】:
标签: asp.net-core .net-core viewmodel paging