Ядро Asp.net как использовать ReflectionIT.Mvc.Paging с ViewModel? - PullRequest
0 голосов
/ 05 июля 2019

Я хочу использовать подкачку с ViewModel в Asp.net Core 2.2.

Вы можете увидеть мой код ниже

public class UserQuestionListComplexViewModel
{
   //There are 2 ViewModel in this Class

    public UserPanelViewModel Model1 { get; set; }
    public List<UserQuestionListViewModel> Model2 { get; set; }
}

И в моем контроллере

public class UserHomeController : Controller
{

 private readonly UserManager<ApplicationUsers> _userManager;
 private readonly IQuestionRepository _iq;

 public UserHomeController(UserManager<ApplicationUsers> userManager, 
        IQuestionRepository iq)
    {
        _userManager = userManager;
        _iq = iq;
    }

    [HttpGet]
    public async Task<IActionResult> QuestionList(UserQuestionListComplexViewModel model,int page = 1)
    {
        var query = _iq.UserQuestionList(_userManager.GetUserId(HttpContext.User),page);
        model.UQVM = await query;
        return View(model);
    }
}

И ниже Архив вопросов

   public async Task<List<UserQuestionListViewModel>> UserQuestionList(string UserID, int page = 1)
    {
        var questionQuery = (from q in _db.QuestionTbl
                             where q.UserID == UserID

                             select new UserQuestionListViewModel()
                             {
                              ....
                             }).AsNoTracking().Where(q => q.qflag == 0).OrderBy(q => q.QuestionID);

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

В конце 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.

1 Ответ

0 голосов
/ 08 июля 2019

Посмотрите на вашу 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<UserQuestionListViewModel> в модели представления:

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