【问题标题】:Asp.net Core how to use ReflectionIT.Mvc.Paging with ViewModel?Asp.net Core 如何将 ReflectionIT.Mvc.Paging 与 ViewModel 一起使用?
【发布时间】: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


    【解决方案1】:

    看你的PagingList创建函数,类型是UserQuestionListViewModel

     var pagedResult = await PagingList<UserQuestionListViewModel>.CreateAsync(questionQuery, 1, page);
    

    但是当您在视图中配置 PagingList 时,您正在设置类型 UserQuestionListComplexViewModel ,因此请替换此行:

     @await this.Component.InvokeAsync("Pager", new { PagingList = this.Model })
    

    与:

     @await this.Component.InvokeAsync("Pager", new { PagingList = this.Model.Model2 })
    

    另外,您可能需要在视图模型中将类型 Model2 更改为 PagingList&lt;UserQuestionListViewModel&gt;

    public PagingList<UserQuestionListViewModel> Model2 { get; set; }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-02
      • 2014-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多