blazor
  1. blazor-bootstrap-modals

Blazor Bootstrap Modals

Bootstrap modals are a popular UI component for displaying content in a modal dialog box. With Blazor, you can easily integrate Bootstrap modals into your applications using the Blazor Bootstrap library.

Syntax

<BModal @bind-Visible="@IsVisible">
    <div class="modal-header">
        <h5 class="modal-title">Modal Title</h5>
        <button class="close" @onclick="@(() => IsVisible = false)">
            <span>&times;</span>
        </button>
    </div>
    <div class="modal-body">
        Modal content goes here...
    </div>
    <div class="modal-footer">
        <button class="btn btn-primary" @onclick="@(() => IsVisible = false)">Save changes</button>
        <button class="btn btn-secondary" @onclick="@(() => IsVisible = false)">Close</button>
    </div>
</BModal>

In this example, we are using the <BModal> component provided by the Blazor Bootstrap library to create a modal dialog box. The @bind-Visible attribute binds the visibility of the modal to a bool property in our code.

Example

@page "/modal-example"

@using BlazorStrap
@using Microsoft.AspNetCore.Components.Web

<h1>Bootstrap Modal Example</h1>

<button class="btn btn-primary" @onclick="ShowModal">Show Modal</button>

<BModal @bind-Visible="@IsVisible">
    <div class="modal-header">
        <h5 class="modal-title">Modal Title</h5>
        <button class="close" @onclick="@CloseModal">
            <span>&times;</span>
        </button>
    </div>
    <div class="modal-body">
        Modal content goes here...
    </div>
    <div class="modal-footer">
        <button class="btn btn-primary" @onclick="@SaveChanges">Save changes</button>
        <button class="btn btn-secondary" @onclick="@CloseModal">Close</button>
    </div>
</BModal>

@code {
    private bool IsVisible = false;

    private void ShowModal()
    {
        IsVisible = true;
    }

    private void CloseModal()
    {
        IsVisible = false;
    }

    private void SaveChanges()
    {
        // Handle save changes
        IsVisible = false;
    }
}

In this Blazor component, we demonstrate how to use the <BModal> component to display a modal dialog box. We use standard Bootstrap classes to style the modal and its components. We also define C# methods to handle the Save Changes button click event and close the modal.

Output

The output of this example will be a button labeled "Show Modal". Clicking this button will display a modal dialog box with the title "Modal Title" and content "Modal content goes here...".

Explanation

Bootstrap modals are a popular UI component for displaying content in a modal dialog box. With Blazor, you can easily integrate Bootstrap modals into your applications using the Blazor Bootstrap library. The <BModal> component provided by this library allows us to create and style modal dialog boxes using standard Bootstrap classes. We can use C# methods to handle button click events and control the visibility of the modal.

Use

Blazor developers can use Bootstrap modals to display content in a modal dialog box, such as confirmation dialogs or forms. They are also useful for displaying large amounts of content that would otherwise clutter the main page.

Important Points

  • The <BModal> component from the Blazor Bootstrap library is used to create modal dialog boxes.

  • Bootstrap classes are used to style the modal and its components.

  • C# methods can be used to handle button click events and control the visibility of the modal.

  • Modals are useful for displaying content in a modal dialog box, such as confirmation dialogs or forms.

Summary

Blazor Bootstrap modals provide a simple and effective way to display content in a modal dialog box. Using standard Bootstrap classes, developers can easily style the modal and its components. C# methods can be used to handle button click events and control the visibility of the modal. Modals are useful for displaying content in a modal dialog box, such as confirmation dialogs or forms.

Published on: