How to pass multiple, optional values in URL on link?
I was able to make it work by using following approach but issue is that what if there are about 15 columns and 15 filters in grid. If you have to add 16th filter than you will have to add line of code 16 times in below example. Or if you have to remove 16th filter than you will end up removing 16 lines of code.
<a asp-page="./Index" asp-route-SortOrder="@Model.First_Name_Sort"
asp-route-SearchString="@Model.SearchString"
asp-route-First_Name="@Model.First_Name"
asp-route-Last_Name="@Model.Last_Name"
asp-route-Location="@Model.Location">`
If I don't include URL variables inside link than it will lost the values of filters. maybe a Model Object would work?
<a asp-page="./Index" asp-route-SortOrder="@Model.First_Name_Sort" >
Display data with sorting
<table>
<thead>
<a asp-page="./Index" asp-route-SortOrder="@Model.First_Name_Sort"
asp-route-SearchString="@Model.SearchString"
asp-route-First_Name="@Model.First_Name"
asp-route-Last_Name="@Model.Last_Name"
asp-route-Location="@Model.Location">
@Html.DisplayNameFor(model => model.CourseTakenList[0].First_Name)
</a>
<a asp-page="./Index" asp-route-SortOrder="@Model.Last_Name_Sort"
asp-route-SearchString="@Model.SearchString"
asp-route-First_Name="@Model.First_Name"
asp-route-Last_Name="@Model.Last_Name"
asp-route-Location="@Model.Location">
@Html.DisplayNameFor(model => model.CourseTakenList[0].First_Name)
</a>
... //another 15 columns
<thead>
...
</table>
Below I have 4 filters
<input type="text" asp-for="SearchString" />
<input type="text" asp-for="First_Name" />
<input type="text" asp-for="Last_Name" />
<input type="text" asp-for="Location" />
.. more filters
Backend code:
[BindProperty(SupportsGet = true)]
public string? SearchString { get; set; }
[BindProperty(SupportsGet = true)]
public string? First_Name { get; set; }
[BindProperty(SupportsGet = true)]
public string? Last_Name { get; set; }
[BindProperty(SupportsGet = true)]
public string? Location { get; set; }
public async Task OnGetAsync()
{
}
CodePudding user response:
Use the asp-all-route-data
attribute and pass a dictionary (https://www.learnrazorpages.com/razor-pages/tag-helpers/anchor-tag-helper#notes):
@{
var routeData = new Dictionary<string, string>{
{ "SortOrder", Model.First_Name_Sort },
{ "SearchString", Model.SearchString },
{ "First_Name", Model.FirstName }
{ "Last_Name", Model.Last_Name },
{ "Location", Model.Location}
};
}
And then
<a asp-page="./Index" asp-all-route-data="routeData">
@Html.DisplayNameFor(model => model.CourseTakenList[0].First_Name)
</a>
<a asp-page="./Index" asp-all-route-data="routeData">
@Html.DisplayNameFor(model => model.CourseTakenList[0].Last_Name)
</a>
... //another 15 columns