Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Bootstrap Toast in .Net Core Web App
Zoltan Halasz
Zoltan Halasz

Posted on

     

Bootstrap Toast in .Net Core Web App

Preliminary knowledge for this short tutorial:

Among my achievements in this transition year was to learn some CSS. This is how I stumbled upon Bootstrap (https://getbootstrap.com/), and later, during my studies of Javascript, I met some JQuery.

One of the nice things in Bootstrap is that it offers some out-of the box elements designed in a simple tasteful way. And, Bootstrap is the default CSS system for .net core (2.2) web applications.

When working on my .net core project, I wanted somehow to send feedback to the user when an action couldn't be accomplished, or there was an error.

This is how I met Toast, which is a nice Html element showing a message in the Browser. You can see examples here:https://www.w3schools.com/bootstrap4/bootstrap_toast.asp

But how can I implement this in my .net core web app?

Here's how I proceeded.

Created a class to contain the basic properties of a Toast

publicclassToaster{publicstringMessage{get;set;}publicstringCssClass{get;set;}}
Enter fullscreen modeExit fullscreen mode

then, within the PageModel class of my page (where I want to show the messages), I declared a binding property, that will contain my message and CssClass (an instance of the above class Toaster)

[BindProperty]publicToastermyToaster{get;set;}
Enter fullscreen modeExit fullscreen mode

I will use this myToaster property to show an error message, when my main list that I want to display in the page, will be empty (no rows are in the list). The below snippet is at the end of the Page OnGet method, and GeneratedDetail is my list that is populated after filtering (see my previous small tutorialhttps://dev.to/zoltanhalasz/filter-a-list-in-razor-pages-net-core-2-2-39bp)

if(GeneratedDetail==null||GeneratedDetail.Count==0)myToaster=newToaster(){CssClass="alert-danger",Message="The list was empty. Try to filter with other values."};elsemyToaster=newToaster(){CssClass="alert-danger",Message=""};returnPage();
Enter fullscreen modeExit fullscreen mode

But, how will then the message arrive in the html of the page? Open the .cshtml file belonging to the pagemodel above, and copy the below snippet, just after the tag, which shows the end of displayed list. This will be a Razor Syntax code with the bootstrap toast and some jquery in the script part below, that will allow the Toast to be shown for 2 seconds.

  @if ((Model.myToaster.Message != "")&& (Model.myToaster != null))        {<divclass="toast mt-3"id="mytoastr"><divclass="toast-body @Model.myToaster.CssClass">                    @Model.myToaster.Message</div></div><script>$(document).ready(function(){$('#mytoastr').toast({delay:2000});$('#mytoastr').toast('show');});</script>        }
Enter fullscreen modeExit fullscreen mode

It is important to have the jquery and bootstrap files in the wwwroot folder! And the project should reference them, so the loaded page should have access to the source of bootstrap.css and jquery.js files.

_Layout file
After running the page, and filtering my data to obtain an empty list, the result will be a small message with red background - "alert-danger" class from Bootstrap, showing "The list was empty. Try to filter with other values."

Toast picture

I hope this was useful for the reader. Let me know if you would do something differently!

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

.Net enthusiast, self-taught, career transition, C#, SQL, Javascript, Angular
  • Location
    Oradea, Romania
  • Education
    Former Management Accountant, Coding enthusiast
  • Work
    Qubiz.com
  • Joined

More fromZoltan Halasz

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp