我剛開始學習如何使用Blazor模板製作網站。但我不知道如何將數據從一個頁面傳遞到另一個頁面。它與.NET CORE MVC Web應用程序有點不同,我找不到這樣的例子。
<p>Solve This problem: @rnd1 * @rnd2 = ?</p>
<input type="text" name="result" bind="@result" />
<input type="button" onclick="@calculate" value="Submit" />
我想將文本框中的值發送到另一個頁面。我怎樣才能做到這一點?
您可以將其作為參數傳遞。
在要導航到的頁面中,將參數添加到您的路線:
@page "/navigatetopage/{myvalue}"
並確保該參數存在於該頁面中:
[Parameter]
private string myvalue{ get; set; }
在同一頁面中,您可以在以下位置進行選擇:
protected override void OnParametersSet()
{
//the param will be set now
var test = myvalue;
}
現在,請確保在起始頁面中導航至第二個頁面,其中包括以下值:
uriHelper.NavigateTo($"/navigatetopage/{result}");
該uriHelper需要這樣注入:
@inject Microsoft.AspNetCore.Blazor.Services.IUriHelper uriHelper
在Preview -9上更新PREVIEW-9 ,您應該使用navigationManager而不是uriHelper,它也具有NavigateTo
方法
@inject Microsoft.AspNetCore.Components.NavigationManager navigationManager