C
C#5mo ago
CrosRoad95

Blazor two way query parameter binding

[Parameter]
[SupplyParameterFromQuery]
public string Category { get; set; }
[Parameter]
[SupplyParameterFromQuery]
public string Category { get; set; }
How can i bind it two way? for example i open page with category=foo, then navigate on the site to category foo and it change query parameter value to bar?
2 Replies
Stroniax
Stroniax5mo ago
I don't know that you're looking for is two-way binding, and it would be more difficult with query parameters. Here's regular two-way binding: Normal two-way binding happens in Blazor by creating a {Parameter}Changed parameter. For example:
C#
[Parameter]
public string? Category { get; set; }
[Parameter]
public EventCallback<string?> CategoryChanged { get; set; }

// Usage
// <MyComponent @bind-Category="_category" />
C#
[Parameter]
public string? Category { get; set; }
[Parameter]
public EventCallback<string?> CategoryChanged { get; set; }

// Usage
// <MyComponent @bind-Category="_category" />
--- What you're doing now should already be binding as you want. Navigation to ?Category=bar will update your Category property's value to bar. To react to that reassignment, override OnParametersSet.
CrosRoad95
CrosRoad955mo ago
This is regular parameter, i want url query parameter