I'm messing about with a Blazor App template created in Visual Studio 2019. I know the project is using the ASP.NET Core 3.0 but what version of Blazor it is, I don't know. It'll be pretty new though as I've only been messing around with it for a few days.
End Result: I was to pass an Id to another Blazor page using a URL parameter.
The bit I'm stuck on: it says on https://docs.microsoft.com/en-us/aspnet/core/blazor/routing?view=aspnetcore-3.0
Use Microsoft.AspNetCore.Components.NavigationManager to work with URIs and navigation in C# code. NavigationManager provides the event and methods shown in the following table. So for that reason, I added an icon with an @onclick event that, when clicked, should use the NavigationManager to go to another page but with an id in the URL.
I get this error for the code below:
Error CS7036 There is no argument given that corresponds to the required formal parameter 'linkAddress' of '__generated__CarsMainView.Navi(string, int)'
Here's my code:
@if (Cars == null)
{
<p><em>Loading...</em></p>
}
else
{
@foreach (var car in Cars)
{
<div class="card">
<img class="card-img-top" src="@car.ImageUrl" alt="Place holder" @onclick="(e => Show(car.Id))" data-toggle="modal" data-target="#carModal" />
<div class="card-body">
<h5 class="card-title">@car.Make @car.Model</h5>
<p class="card-text">@car.Description</p>
<a href="@car.Url" class="btn btn-primary">Wiki</a>
<span class="fa fa-fighter-jet" @onclick="(() => Navi("carMoved", car.Id)"></span>
</div>
<div class="card-footer">
<button class="btn btn-secondary"></button>
</div>
</div>
}
}
@code {
private void Navi(string linkAddress, int id)
{
navigationManager.NavigateTo($"{linkAddress}/{id}");
}
List<CarDetail> Cars;
CarDetail CarToInsert;
protected async Task Load()
{
Cars = await carService.GetCarsAsync();
}
protected override async Task OnInitializedAsync()
{
await Load();
CarToInsert = new CarDetail();
}
}
In the code above I want to send a string value containing the address to another page and an int value with the ID of the car. these values are passed into Navi(string, int) which should then direct me to that page. Problem is the string value is gives me the error mentioned above.
I know what you're thinking... I should use single quotes in my @onclick, so it would look like this instead.
@onclick="(() => Navi('carMoved', 1)"
I tried that. Problem is I get this error:
Too many characters in character literal
Any help would be much appreciated
You can use a lambda expression to pass multiple arguments to an onclick event.
By using Blazor route parameters, you can pass values from one page to another page. Use NavigationManager to pass a value and route the parameter to another page. Follow this example to achieve passing values from one page to another. Get the passed Page1 parameter value in Page2.
Blazor does not have support to manipulate DOM elements directly, but we can still achieve it by using JavaScript interop. By creating a reference to an element, we can send it as a parameter to the JavaScript method via interop.
You can redirect to a page in Blazor using the Navigation Manager's NavigateTo method. In the following code snippet, it will redirect to the home page when this page gets loaded. Similarly, you can call NavigateTo() method from NavigationManager class anywhere to redirect to another page.
Right... I'm a muppet.
I just changed the quotes around so it went from this:
<span class="fa fa-fighter-jet" @onclick="(() => Navi("carMoved", 1))">
</span>`
this this:
`<span class="fa fa-fighter-jet" @onclick='(() => Navi("carMoved", 1))'>
</span>`
Now it works.
I would go with an even more readable way so you don't need those extra quotes around:
<span class="fa fa-fighter-jet" @onclick="@(e => Navi("carMoved", 1))"></span>
The same syntax also described here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With