首页 > 解决方案 > 在 ASP.Net Core 中使用分页传递模型参数

问题描述

我正在使用 PagedList 为我的 ASP.Net Core 应用程序处理表。我需要在页面更改时将模型传递给控制器​​,但是它没有被发送。

视图中代码的分页部分是

<pager class="pager" list="@Model.UserList" asp-action="Index" asp-controller="User" asp-route-model ="@Model" param-page-number="page" options="@PagedListRenderOptions.ClassicPlusFirstAndLast" />

我的控制器签名是:

public ActionResult Index(UsersModel model, int? page)
{
}

模型始终为 null,并且不包含任何模型值。我如何解决它?

标签: c#asp.net-corepaginationpagedlist

解决方案


您无法使用asp-route-{value}or传递整个模型对象asp-all-route-data

asp-route用于string类型,asp-all-route-data用于IDictionary<string,string>类型

我建议您可以手动传递模型数据,例如

@{
var parms = new Dictionary<string, string>
            {
                { "Id",Model.Id.ToString() },
                {"Name",Model.Name },
                //...
            };
}

<a asp-action="Index" asp-controller="User" asp-all-route-data="parms">Test</a>

它将作为查询字符串工作。

参考https://docs.microsoft.com/en-us/aspnet/core/mvc/views/tag-helpers/built-in/anchor-tag-helper?view=aspnetcore-3.0#asp-all-route-数据

https://forums.asp.net/t/1984183.aspx?Passing+Model+Object+in+route+values+of+Action+link


推荐阅读