Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Core Blazor App: How to pass data between pages?

Tags:

I just started learning how to make websites with using Blazor template. But I don't know how to pass the data from one page to another. It is a bit different than .NET CORE MVC web application and I couldn't find an example for this.

    <p>Solve This problem: @rnd1 * @rnd2 = ?</p>

    <input type="text" name="result" bind="@result" />
    <input type="button" onclick="@calculate" value="Submit" />

I want to send the value in my textbox to the another page. How can I do this?

like image 580
jorjj Avatar asked Jul 07 '18 19:07

jorjj


People also ask

How do you pass data between pages in Blazor?

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.

How do you pass data from parent to child in Blazor?

When you call callApi in the FirstChild , it will change it's own data , if you want to change the parent's data , you need to pass a parameter to FirstChild which will be a function to call when the span is clicked and that will call the Parent 's callApi .

How does Blazor store data?

First to access browser seesionStorage in Blazor apps, write a custom code or use a third party package. The accessed data can be stored in the localStorage and sessionStorage. The localStorage is scoped to the user's browser.


1 Answers

You can pass it as a parameter.

In the page you want to navigate to, add the parameter to your route:

@page "/navigatetopage/{myvalue}"

and make sure the parameter exists in that page:

[Parameter]
private string myvalue{ get; set; }

In the same page you can pick that up in:

protected override void OnParametersSet()
{
    //the param will be set now
    var test = myvalue;
}

Now in your start page make sure to navigate to the second page including the value:

uriHelper.NavigateTo($"/navigatetopage/{result}");

That uriHelper needs to be injected like this:

@inject Microsoft.AspNetCore.Blazor.Services.IUriHelper uriHelper

UPDATE PREVIEW-9 on preview-9 you should use navigationManager instead of uriHelper, it also has a NavigateTo method

@inject Microsoft.AspNetCore.Components.NavigationManager navigationManager
like image 63
Flores Avatar answered Sep 27 '22 17:09

Flores