Tag helper in _layout.cshtml page for start up after website load

There are a lot of similar questions but not the one that give me the solution.

This is my attempt: I want to when the website loads show a alert Modal box with data coming from a model.
The site is a Blazor Server app.

This the model

public class AlertModal
{

    [Key]
    public int AlertId { get; set; }
    public string Image { get; set; }

    [Required(ErrorMessage = "This is a requierd field")]
    public string TextPar { get; set; } 
    public string TextPar1 { get; set; }
    public string TextPar2 { get; set; }

    [Required(ErrorMessage = "This is a requierd field")]
    public bool Show { get; set; }
}

This the PageModel of the partial page:

public class _InfoPartialModel : PageModel
{
    public AlertModal AlertModal { get; set; }
    public void OnGet()
    {
        AlertModal = new AlertModal()
        {
            TextPar = "This is a test",
            Show = true
        };
    }
}

And this the partial view page

   @page
@model Tjoba.Pages.Shared._InfoPartialModel
@{
}

@if (Model.AlertModal.Show)
{
    <div class="modal fade " id="IndexModal" role="dialog" tabindex="-1" aria-hidden="true">
        <div class="modal-dialog modal-dialog-centered" role="document">
            <div class="modal-content">

                <div class="modal-header">
                    <h5 class="modal-title text-black">@Model.AlertModal.TextPar </h5>
                    <button type="button" data-bs-dismiss="modal" aria-hidden="true">x</button>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">×</span>
                    </button>
                </div>

                <div class="modal-body">
                    <div class="row">
                        <img src="/images/Vacation_volleyball.jpeg" class=" col-12 p-0 m-0 mx-auto " style="transform:none" />
                    </div>
                </div>
                <div class="modal-footer">
                    <button class="btn btn-secondary" value="1" data-bs-dismiss="modal">Sluiten</button>

                </div>
            </div>
        </div>
    </div>
    <script>
        $(document).ready(function () {

            setTimeout(function () {
                $("#IndexModal").modal('show');
            }, 4000);           

        });
    </script>
}

And for startup in the _Layout.cshtml page:

<partial name="_InfoPartial" model="Model.AlertModal" />

I found the explanation of this here:

But when starting the website there comes a the following error:
An unhandled exception occurred while processing the request.
RuntimeBinderException: ‘Tjoba.Pages.Pages__Host’ does not contain a definition for ‘AlertModal’

The _Host.cshtml file:

@page "/"
@namespace Tjoba.Pages

@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers


@{
    Layout = "_Layout";

}

<component type="typeof(App)" render-mode="ServerPrerendered" />

Question is, what as the _Host file to do with this?. I do what wrong but I can’t find what it is.

To achieve your goal of displaying a modal box on startup in a Blazor Server app, you can use a combination of Blazor components and Razor views. Given your current setup, it seems you are trying to mix Blazor components and Razor pages. Here’s a refined approach:

  1. Create a Blazor Component for the Modal: Create a new Blazor
    component for the modal. This component will handle the display
    logic for the modal box.
  2. Add the Modal Component to the Main Layout: Incorporate the modal
    component into the _Layout.cshtml page to ensure it loads on
    startup.
New contributor

Khach is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Test Result

The structure of my test project

1. Data/AlertModal.cs

using System.ComponentModel.DataAnnotations;

namespace BlazorApp2.Data
{
    public class AlertModal
    {
        [Key]
        public int AlertId { get; set; }
        public string? Image { get; set; }

        [Required(ErrorMessage = "This is a required field")]
        public string? TextPar { get; set; }
        public string? TextPar1 { get; set; }
        public string? TextPar2 { get; set; }

        [Required(ErrorMessage = "This is a required field")]
        public bool Show { get; set; }
    }

}

2. AlertModalComponent.cs

@page "/alert-modal"
@using BlazorApp2.Data
@inject AlertModalService AlertModalService

@code {
    private AlertModal AlertModal { get; set; }

    protected override void OnInitialized()
    {
        AlertModal = AlertModalService.GetAlertModal();
    }
}

@if (AlertModal.Show)
{
    <div class="modal fade" id="IndexModal" role="dialog" tabindex="-1" aria-hidden="true">
        <div class="modal-dialog modal-dialog-centered" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title text-black">@AlertModal.TextPar</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>

                <div class="modal-body">
                    <div class="row">
                        <img src="@AlertModal.Image" style="width:72px;height:50px" />
                    </div>
                </div>
                <div class="modal-footer">
                    <button class="btn btn-secondary" data-bs-dismiss="modal">关闭</button>
                </div>
            </div>
        </div>
    </div>
    <script>
        document.addEventListener('DOMContentLoaded', function () {
            setTimeout(function () {
                var modal = new bootstrap.Modal(document.getElementById('IndexModal'));
                modal.show();
            }, 4000);
        });
    </script>
}

3. _Host.cshtml

@page "/"
@using Microsoft.AspNetCore.Components.Web
@namespace BlazorApp2.Pages
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

<!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="css/bootstrap/bootstrap.min.css" />
    <link href="css/site.css" rel="stylesheet" />
    <link href="BlazorApp2.styles.css" rel="stylesheet" />
    <link rel="icon" type="image/png" href="favicon.png" />
    

    <component type="typeof(HeadOutlet)" render-mode="ServerPrerendered" />
</head>
<body>
    <component type="typeof(App)" render-mode="ServerPrerendered" />
    @* Add this line *@
    <component type="typeof(AlertModalComponent)" render-mode="ServerPrerendered" />

    <div id="blazor-error-ui">
        <environment include="Staging,Production">
            An error has occurred. This application may no longer respond until reloaded.
        </environment>
        <environment include="Development">
            An unhandled exception has occurred. See browser dev tools for details.
        </environment>
        <a href="" class="reload">Reload</a>
        <a class="dismiss">????</a>
    </div>

    <script src="_framework/blazor.server.js"></script>
    <script src="~/js/popper.min.js"></script>
    <script src="~/js/bootstrap.min.js"></script>
</body>
</html>

4. AlertModalService.cs

using BlazorApp2.Data;

namespace BlazorApp2
{
    public class AlertModalService
    {
        public AlertModal GetAlertModal()
        {
            return new AlertModal()
            {
                TextPar = "This is a test",
                Show = true,
                Image = "/images/test.png"
            };
        }
    }

}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật