TheFallenOne
TheFallenOne

Reputation: 1686

To pass model parameter using pagination in ASP.Net Core

I am using the PagedList to work on a table for my ASP.Net Core application. I need to pass the model to the controller on change of a page, however it's not being sent.

The pagination section of the code in the view is

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

And my controller signature is :

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

The model is always null and does not contain any of the model values. How do I fix it?

Upvotes: 2

Views: 3289

Answers (1)

Ryan
Ryan

Reputation: 20116

You could not pass the whole model object using asp-route-{value} or asp-all-route-data.

asp-route is for string type and asp-all-route-data is for IDictionary<string,string> type

I suggest that you could pass model data manually like

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

It will work as queryString.

Refer to https://learn.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

Upvotes: 2

Related Questions