How do I display a custom bootstrap alert from backend in blazor server

Cenk 1,021 Reputation points
2022-08-08T19:54:30.797+00:00

Hi,

In my Blazor Server App, I want to display any kind of success alert after saving it into the database. How can I do this with the bootstrap alert in the example below? (Similarly, the error message may be.)

async Task SaveRow(Order? order)  
    {  
        if (order == _orderToInsert)  
        {  
            _orderToInsert = null;  
        }  
  
        await _grid.UpdateRow(order);  
  
        _orders = await ViewAllOrdersUseCase.ExecuteAsync();  
       StateHasChanged();  
    }  
Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,595 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 66,461 Reputation points
    2022-08-08T20:17:23.487+00:00

    on the view that define the alerts use an if with a component variable.

    @code {     
        [Parameter]     
        public bool DisplayAlert { get; set; }     
    }     
       
       
       
       
    @if (DisplayAlert) {   
        <div class="alert alert-success fade-in" role="alert">   
          This is a success alert—check it out!   
        </div>   
    }   
       
    
       
    
    
       
    
    0 comments No comments

  2. AgaveJoe 28,536 Reputation points
    2022-08-08T20:45:05.003+00:00

    Basic Example.

    @page "/alert"  
      
    <h3>Bootstrap Alert Example</h3>  
      
    <div>  
        <button @onclick="@DoSomthing">Show/Hide</button>  
    </div>  
      
    <div class="alert alert-primary @HideAlert" role="alert" >  
      @AlertContent  
    </div>  
      
    @code {  
        private string HideAlert {get; set;} = "d-none";  
        private string AlertContent { get; set; } = "This is a primary alert—check it out!";  
        private void DoSomthing()  
        {  
            if (HideAlert == "")  
            {  
                HideAlert = "d-none";  
                AlertContent = "This is a primary alert—check it out!";  
            }  
            else  
            {  
                HideAlert = "";  
                AlertContent = "Hello World";  
            }     
        }    
    }  
    

    Going forward, share the code you've tried.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.