最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

asp.net core - Blazored Modal (.NET 9 Blazor )- object reference not set to an instance of an object - Stack Overflow

programmeradmin5浏览0评论

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

Share edited Mar 17 at 7:06 Zhi Lv 22k1 gold badge27 silver badges37 bronze badges asked Mar 14 at 13:58 sTxsTx 1,27513 silver badges36 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

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>

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论