.NET Core Blazor 应用程序:如何在页面之间传递数据? [英] .NET Core Blazor App: How to pass data between pages?
问题描述
我刚刚开始学习如何使用 Blazor 模板制作网站.但我不知道如何将数据从一页传递到另一页.它与 .NET CORE MVC Web 应用程序有点不同,我找不到这方面的示例.
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?
推荐答案
您可以将其作为参数传递.
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}");
那个uriHelper需要像这样注入:
That uriHelper needs to be injected like this:
@inject Microsoft.AspNetCore.Blazor.Services.IUriHelper uriHelper
更新 PREVIEW-9在 preview-9 上你应该使用 navigationManager 而不是 uriHelper,它还有一个 NavigateTo
方法
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
这篇关于.NET Core Blazor 应用程序:如何在页面之间传递数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!