Bootstrap 5 Solution --> Tested with .NET Core 5.0
The bootstrap alert HTML code (these are stored in a Razor View Component)
<!-------------------------------->
<!-- INFORMATION Bootstrap Alert-->
<!-------------------------------->
<div class="alert alert-info alert-dismissible fade show bg-info" role="alert" id="bootstrapInformationAlertDiv" style="display:none;">
<div class="custom-alert-div-information text-light font-weight-bold text-dark" id="bootstrapInformationAlertMessageDiv"> </div>
<button type="button" class="btn-close" aria-label="Close" id="bootstrapInformationAlertBtn"></button>
</div>
<!---------------------------->
<!-- SUCCESS Bootstrap Alert-->
<!---------------------------->
<div class="alert alert-success alert-dismissible fade show bg-success" role="alert" id="bootstrapSuccessAlertDiv" style="display:none;">
<div class="custom-alert-div-success text-light font-weight-bold" id="bootstrapSuccessAlertMessageDiv"> </div>
<button type="button" class="btn-close" aria-label="Close" id="bootstrapSuccessAlertBtn"></button>
</div>
<!---------------------------->
<!-- WARNING Bootstrap Alert-->
<!---------------------------->
<div class="alert alert-warning alert-dismissible fade show bg-warning" role="alert" id="bootstrapWarningAlertDiv" style="display:none;">
<div class="custom-alert-div-warning text-black font-weight-bold" id="bootstrapWarningAlertMessageDiv"> </div>
<button type="button" class="btn-close" aria-label="Close" id="bootstrapWarningAlertBtn"></button>
</div>
<!--------------------------->
<!-- DANGER Bootstrap Alert-->
<!--------------------------->
<div class="alert alert-danger alert-dismissible fade show bg-danger" role="alert" id="bootstrapDangerAlertDiv" style="display:none;">
<div class="custom-alert-div-danger text-light font-weight-bold" id="bootstrapDangerAlertMessageDiv"> </div>
<button type="button" class="btn-close" aria-label="Close" id="bootstrapDangerAlertBtn"></button>
</div>
The above view component is added at the top of each web page:
<!-- Bootsrap Alert View Component -->
<div>
@await Component.InvokeAsync("BootstrapAlert")
</div>
I then added a small JS file to my project (bootstrapAlert.js):
$("#bootstrapInformationAlertBtn").click(function () {
event.preventDefault();
$(this).parent().hide();
});
$("#bootstrapSuccessAlertBtn").click(function () {
event.preventDefault();
$(this).parent().hide();
});
$("#bootstrapWarningAlertBtn").click(function () {
event.preventDefault();
$(this).parent().hide();
});
$("#bootstrapDangerAlertBtn").click(function () {
event.preventDefault();
$(this).parent().hide();
});
In my main Layout.cshtml file I add the script ref to the above js file so it's auto loads with each page in my site:
<!-- Load the js script file on each page for hiding the bootstrap alert when closed -->
<script src="~/js/bootstrap-alert-custom-scripts/bootstrapAlert.js"></script>
When calling a bootstrap alert to open from any of my javascript functions, I use the following code:
window.scrollTo(0, 0); // Scroll to the top of the page.
document.getElementById("bootstrapWarningAlertMessageDiv").innerHTML = 'Write your custom message here for the current issue / notification!';
$("#bootstrapWarningAlertDiv").show();
setTimeout(function () { $("#bootstrapWarningAlertDiv").hide(); }, 8000);
The window scroll to command is a nice little touch because the user may be scrolled some way down the page and if you're always adding the alert in the same place i.e. at the top of the page, then they wouldn't necessarily see it when it opens up.