I'm using Razor Pages 7. My page has filtering, sorting and paging.
MyPage.cshtml.cs
public SelectList Products { get; set; }
public int Sort { get; set; }
public int Page { get; set; }
public int Filter { get; set; }
public async Task OnGetAsync(int sort, int page, int filter) {
//...
}
MyPage.cshtml
<form method="get" asp-page="" asp-route-sort=@Model.Sort asp-route-page=@Model.Page>
<select name="filter" asp-items=@Model.Products>
<option selected value="0">Filter by product</option>
</select>
<input type="submit" value="submit" />
</form>
That correctly renders as:
<form method="get" action="/mypage?sort=10&page=3">
...
</form>
But when I submit the form, the get action only binds the filter
argument. The sort
and page
arguments are not bound from the form's action
.
Does RazorPages offer a way to bind those too?
(I could do it with JavaScript, but I'm hoping there's a RazorPages-native approach.)
UPDATE
I found a way, using hidden inputs. In the form:
<input type="hidden" name="sort" value=@Model.Sort />
<input type="hidden" name="page" value=0 /> <!-- reset page on filter change -->
But this is a manual approach with many magic strings. Is there a RazorPages "native" way?