ViewComponent InvokeAsync获取错误“包含通用参数参数名称:类型”

时间:2019-06-22 02:28:13

标签: asp.net-core asp.net-core-2.2

我为数据列表分页创建了一个ViewComponent,但是,当我调用InvokeAsync方法时,却出现此错误:

ArgumentException: Type MyApp.Domain.Core.Interfaces.Data.Paging.IPaginate`1[T] contains generic parameters
 Parameter name: type

与泛型类型不兼容吗?这是代码:

查看(简体):

@model IPaginate<MyApp.Models.CurrencyRateViewModel>

@(await Component.InvokeAsync<PagerViewComponent>(Model))

ViewComponent cshtml:

@using MyApp.Domain.Core.Interfaces.Data
@model  PagerViewModel
@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
    var startIndex = Math.Max(Model.Index - (Model.MaxPagesToDisplay / 2), 1);
    var finishIndex = Math.Min(Model.Index + (Model.MaxPagesToDisplay / 2), Model.Pages);
}
<span class="pull-left">Pagina @(Model.Pages < (Model.Index + 1) ? 0 : (Model.Index + 1)) de @Model.Pages</span>

<ul class="pagination pagination-sm no-margin pull-right">
    @{
        if (Model.HasPrevious)
        {
            <li>
                <a asp-controller="TestDirection" asp-action="Index"
                   asp-route-pageNumber="@(Model.Index)">&laquo;</a>
            </li>
        }

        @for (var i = startIndex; i <= finishIndex; i++)
        {
            <li class="@(Model.Index + 1 == i ? "active" : "")">
                <a asp-controller="TestDirection" asp-action="Index"
                   asp-route-pageNumber="@(i)">@i</a>
            </li>
        }

        if (Model.HasNext)
        {
            <li>
                <a asp-controller="TestDirection" asp-action="Index" asp-route-pageNumber="@(Model.Index + 2)">&raquo;</a>
            </li>
        }
    }
</ul>

ViewComponent CS:

public class PagerViewComponent : ViewComponent
    {
        private readonly IMapper _mapper;

        public PagerViewComponent(IMapper mapper)
        {
            _mapper = mapper;
        }

        public Task<IViewComponentResult> InvokeAsync<T>(IPaginate<T> result, int maxPagesToDisplay = 5)
        {
            var model = _mapper.Map<PagerViewModel>(result);
            model.MaxPagesToDisplay = maxPagesToDisplay;

            return Task.FromResult((IViewComponentResult)View("Default", model));
        }
    }

IPaginate.cs

public interface IPaginate<T>
    {
        int From { get; }

        int Index { get; }

        int Size { get; }

        int Count { get; }

        int Pages { get; }

        IList<T> Items { get; }

        bool HasPrevious { get; }

        bool HasNext { get; }
    }

我该如何解决?

0 个答案:

没有答案