大火中事件的错误序列
我有一个大餐服务器端应用程序。 的组件中的.razor.cs中的代码中
protected override async Task OnInitializedAsync()
{
...
_role = await Authorization.GetCurrentUserRoleAsync();
QuoteModel = await QuotesDb.GetQuoteByIdAsync(_role, QuoteId);
在我在.RAZOR中
@if((DateTime.Now - QuoteModel.Date).TotalDays > 60)
{
... do something
}
:在运行应用程序时,
_role = await Authorization.GetCurrentUserRoleAsync();
在代码范围内执行,然后执行剃须刀
@if((DateTime.Now - QuoteModel.Date).TotalDays > 60)
,然后执行在code-behand中,在code-behind中实例化,当然,这会导致无效参考异常。
如何在使用之前确保实例化被布置?
I have a Blazor server-side application. In the code-behind in .razor.cs of a component I have
protected override async Task OnInitializedAsync()
{
...
_role = await Authorization.GetCurrentUserRoleAsync();
QuoteModel = await QuotesDb.GetQuoteByIdAsync(_role, QuoteId);
In the .razor:
@if((DateTime.Now - QuoteModel.Date).TotalDays > 60)
{
... do something
}
When the application is run,
_role = await Authorization.GetCurrentUserRoleAsync();
in the code-behind is executed, and then in razor
@if((DateTime.Now - QuoteModel.Date).TotalDays > 60)
is executed, before QuoteModel is instantiated in code-behind, which of course causes a null reference exception.
How can I make sure QuoteModel is instantiated before it is used?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过NULL检查处理:
You can handle that with a null check: