如何将对象从一个页面组件传递到.NET Blazor应用中的另一页组件?
我有一个.NET Blazor Server应用程序,需要将对象从一个组件传递到另一个组件。这两个组件都是页面,这意味着它们具有带路线的@page指令。我知道如何使用级联值在常规的大火组件之间传递参数,但这与页面组件不起作用。我也知道如何在端点路由中传递参数。但是,我不想传递具有多个属性的对象,并且不确定如何最好地实现这一目标。
是否可以将对象作为端点路由参数传递?如果不是,那么在剃须乐组件上下文中实现这一目标的好方法是什么?
I have a .NET Blazor Server app and need to pass an object from one component to another. Both components are pages, meaning that they have @page directives with routes. I know how to use cascading values to pass a parameter between regular Blazor components, but this does not work with page components. I also know how to pass a parameter within an endpoint route. However, instead of a string or int I want to pass an object with multiple properties and am unsure how to best accomplish this.
Is it possible to pass an object as an endpoint route parameter? If not, what is a good way to accomplish this within a Razor components context?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用依赖注入可能会为您解决此问题。
示例:
您可以在“ applicationservice.cs”文件中的applicationservice类中具有类似的内容
您可以拥有这样的东西:
您可以在_imports.razor中有类似的东西
@Inject Applicationservice MainAppService;
现在在剃须刀组件中,您应该能够做类似的事情:
等待mainappservice.msgbox(“这是一个消息框”);
这在我的WASM Blazor应用程序中起作用,希望它能在服务器端阐明
Using dependency injection would likely solve this issue for you.
Example:
You could have something like this
You could have something like this:
You could have something like this
In your _Imports.razor you can inject the class so that the pages have access to it:
@inject ApplicationService MainAppService;
Now in your razor components you should be able to do something like this:
await MainAppService.MsgBox("This is a message box");
This works in my WASM blazor app, hope it sheds some light on the server side of things ????
使用DI服务。创建一个班级以保持您的对象。将其添加为程序中的范围服务。在任何组件中使用它(页面只是具有页面属性的组件),但
@inject
。Use a DI service. Create a class to hold your object. Add it as a Scoped Service in Program. Use it in any component (pages are just components with a page attribute) though
@inject
.