admin管理员组文章数量:1389772
I'm trying to implement Blazored Modal exactly as described here in a
@rendermode InteractiveServer
page. When I test
<button @onclick="@(() => Modal.Show<TestComp>("My Test Compnent"))">View TestC</button>
I get the following error:
System.NullReferenceException: Object reference not set to an instance of an object.
because Modal
is null
, although I've defined it like in docs:
[CascadingParameter]
public IModalService Modal { get; set; } = default!;
TestComp
is just an automatically generated component (with a h3
tag) - I thought that maybe this modal only works with nonpage components.
I can't figure out what I'm doing wrong...
PS: I've seen in an old video to use @inject IModalService Modal
instead of CascadingPrameter
. This way the Model
is not null anymore but modal is still not showing up
<button @onclick="ShowModal">View Movies</button>
// [CascadingParameter] public IModalService Modal { get; set; } = default!;
private async Task ShowModal()
{
var moviesModal = Modal.Show<TestComp>("My Movies");
var result = await moviesModal.Result; // breakpoint exit here without anything happening (basically it's waiting for a Result...)
if (result.Cancelled)
{
Console.WriteLine("Modal was cancelled");
}
else if (result.Confirmed)
{
Console.WriteLine("Modal was closed");
}
}
I've also added the <BlazoredModal />
component in MainLayout.razor
but it's still not working
I'm trying to implement Blazored Modal exactly as described here in a
@rendermode InteractiveServer
page. When I test
<button @onclick="@(() => Modal.Show<TestComp>("My Test Compnent"))">View TestC</button>
I get the following error:
System.NullReferenceException: Object reference not set to an instance of an object.
because Modal
is null
, although I've defined it like in docs:
[CascadingParameter]
public IModalService Modal { get; set; } = default!;
TestComp
is just an automatically generated component (with a h3
tag) - I thought that maybe this modal only works with nonpage components.
I can't figure out what I'm doing wrong...
PS: I've seen in an old video to use @inject IModalService Modal
instead of CascadingPrameter
. This way the Model
is not null anymore but modal is still not showing up
<button @onclick="ShowModal">View Movies</button>
// [CascadingParameter] public IModalService Modal { get; set; } = default!;
private async Task ShowModal()
{
var moviesModal = Modal.Show<TestComp>("My Movies");
var result = await moviesModal.Result; // breakpoint exit here without anything happening (basically it's waiting for a Result...)
if (result.Cancelled)
{
Console.WriteLine("Modal was cancelled");
}
else if (result.Confirmed)
{
Console.WriteLine("Modal was closed");
}
}
I've also added the <BlazoredModal />
component in MainLayout.razor
but it's still not working
1 Answer
Reset to default 0In all probability you have Interactivity set to Per Page/Component which means that the router and the layout components are being rendered statically.
App
should look like this. Note the render mode set on HeadOutlet
and Routes
.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<base href="/" />
<link rel="stylesheet" href="@Assets["lib/bootstrap/dist/css/bootstrap.min.css"]" />
<link rel="stylesheet" href="@Assets["app.css"]" />
<link rel="stylesheet" href="@Assets["xxxxx.styles.css"]" />
<ImportMap />
<link rel="icon" type="image/png" href="favicon.png" />
<HeadOutlet @rendermode="InteractiveServer" />
</head>
<body>
<Routes @rendermode="InteractiveServer" />
<script src="_framework/blazor.web.js"></script>
</body>
</html>
本文标签:
版权声明:本文标题:asp.net core - Blazored Modal (.NET 9 Blazor )- object reference not set to an instance of an object - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744652609a2617775.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论