使用分页TagHelper进行模型绑定

时间:2018-04-04 21:18:29

标签: c# asp.net-core pagination

我有以下PageLinkTagHelper.cs为项目列表生成分页链接

[HtmlTargetElement("div", Attributes = "page-model")]
    public class PageLinkTagHelper : TagHelper
    {
        private IUrlHelperFactory urlHelperFactory;

        public PageLinkTagHelper(IUrlHelperFactory helperFactory)
        {
            urlHelperFactory = helperFactory;
        }

        [ViewContext]
        [HtmlAttributeNotBound]
        public ViewContext ViewContext { get; set; }

        public PagingInfo PageModel { get; set; }

        public string PageAction { get; set; }

        public bool PageClassesEnabled { get; set; } = false;
        public string PageClass { get; set; }
        public string PageClassNormal { get; set; }
        public string PageClassSelected { get; set; }

        public override void Process(TagHelperContext context,
                TagHelperOutput output)
        {
            IUrlHelper urlHelper = urlHelperFactory.GetUrlHelper(ViewContext);
            TagBuilder result = new TagBuilder("div");
            for (int i = 1; i <= PageModel.TotalPages; i++)
            {
                TagBuilder tag = new TagBuilder("a");
                tag.Attributes["href"] = urlHelper.Action(PageAction,
                   new { listPage = i });
                if (PageClassesEnabled)
                {
                    tag.AddCssClass(PageClass);
                    tag.AddCssClass(i == PageModel.CurrentPage
                        ? PageClassSelected : PageClassNormal);
                }
                tag.InnerHtml.Append(i.ToString());
                result.InnerHtml.AppendHtml(tag);
            }
            output.Content.AppendHtml(result.InnerHtml);
        }
    }

我正在努力修改它以使用模型绑定。我想在以下方法中使用它:

public IActionResult SearchResult(SearchResultViewModel viewModel, int listPage =1)
        {
            if (viewModel != null)
            {
                List<Form> searchResult = GetSearchResult(viewModel.SearchTerms).ToList();
                var model = new SearchResultViewModel
                {
                    Forms = searchResult
                        .OrderBy(f => f.FormID)
                        .Skip((listPage - 1) * PageSize)
                        .Take(PageSize),
                    SearchTerms = new SearchTerms
                    {
                        //search criteria deleted for brevity
                    },
                    PagingInfo = new PagingInfo
                    {
                        CurrentPage = listPage,
                        ItemsPerPage = PageSize,
                        TotalItems = searchResult.Count()
                    }
                };

                return View("SearchResult", model);
            }
            return View("SearchForms");
        }

生成的链接为localhostXXXX/Form/SearchResult?listPage=#,其中生成了正确的列表页面。但是,单击链接时传递给控制器​​的SearchResultViewModel为空。这具有返回存储库中的所有项目的效果(因为没有要搜索的搜索项)。如何使用此分页绑定我的SearchResultViewModel.cs对象?

我的观点是

@model SearchResultViewModel
@foreach (var form in Model.Forms)
{
    @Html.Partial("FormSummary", form)
}

<div page-model="@Model.PagingInfo" page-action="SearchResult" page-classes-enabled="true" page-class="btn" page-class-selected="btn-primary" class="btn-group pull-right m-1"></div>

1 个答案:

答案 0 :(得分:0)

如果您不希望SearchResultViewModel为空,则需要传递localhostXXXX/Form/SearchResult?PagingInfo.CurrentPage=#之类的内容。在这种情况下,您要设置SearchResultViewModel的属性。