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
In 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>