【问题标题】:To pass model parameter using pagination in ASP.Net Core在 ASP.Net Core 中使用分页传递模型参数
【发布时间】:2020-02-01 02:43:14
【问题描述】:

我正在使用 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,并且不包含任何模型值。我该如何解决?

【问题讨论】:

  • 你使用哪个pagedlist包?

标签: c# asp.net-core pagination pagedlist


【解决方案1】:

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

asp-route 用于string 类型,asp-all-route-data 用于IDictionary&lt;string,string&gt; 类型

我建议你可以像手动传递模型数据

@{
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-data

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

【讨论】:

  • 好答案!此外,如果您需要传递额外的路由参数,请在 asp-all-route-data 之后(而不是之前!)执行。例如:测试
猜你喜欢
  • 2017-08-25
  • 2021-09-30
  • 2021-01-21
  • 2019-01-30
  • 1970-01-01
  • 1970-01-01
  • 2018-03-24
  • 2022-09-27
  • 2013-01-05
相关资源
最近更新 更多