DEV Community

loading...
Cover image for How to build a toastr.js notification system using ASP.NET MVC

How to build a toastr.js notification system using ASP.NET MVC

Cory Harkins
Just a web dev trying to code my way through the world!
Updated on ・3 min read

SRC: https://coryharkins.medium.com/how-to-implement-a-toastr-js-notifications-system-in-asp-net-mvc-5-d755a387ac5b

Hello and welcome!

Today we are going to learn how to implement a server side generated notification system using the following:

  • MVC 5
  • C#
  • ASP.NET
  • Toastr.js

Setup
To get this set up you can do a couple different things. jQuery is a requirement for this set up but a default MVC project comes with that out of the box so I won’t go over how to set that up here.
First you’ll need to add a reference to toastr.js. You can do so by installing the nuget package for toastr or by clicking here and using the cnd versions. I went with the nuget install and added the references where they were needed.

Your bundle config should look something like this with a reference to toastr:

bundles.Add(new ScriptBundle("~/bundles/toastr").Include( "~/Scripts/toastr.js"));
bundles.Add(new StyleBundle("~/Content/css").Include( "~/Content/toastr.css"));

Then add the render call for your script and css files in your layout page (or where ever you need this script to be firing):


@Styles.Render("~/Content/css")
//body code and other stuff pertaining to layout
@Scripts.Render("~/bundles/toastr")

The Fun Stuff
Ok so now that the boring setup stuff is out of the way. Let’s get down to business.

I first created a new folder for this helper to live in. I called it “Helpers” ( I know, clever). Inside that I made a class called NotificationsHelper.cs.

This class is a view method calls and a model. We will utilize ASP.NET session variables to gather up a list of notifications to process as your code runs then process and render them in the layout with another method call.

Ideally only one or two notifications at most would be toasted to the user but you can go crazy and get a list of 10+ if you want to drive your users mad!

Alright, back to business. Here is the class I’ve set up.

I’ll link the source here.
We can analyze the workflow of things below.

Render Notifications
The render notifications method does exactly that. You call this on your layout page, and any time the layout page is rendered this system will auto populate any toasts that need to send.
It pulls the data from a Session variable that I’ve chosen to call “Notifications” that takes in a List of type Notification that were collected during any Controller calls. It serializes this list to json and stores that in the session.
If there is anything to render is generates basic toastr.js code to pop up an error or success message.

Clear
The clear method just empties the session object once it’s been processes by RenderNotifications();

Add Notification
The add notification method takes in a Notification object that consists of a string message, a string title, and an enum notification type.

This method can be called in any controller and it will store the notification in the session object. Which is then serialized and stored.

Definitions
Notification type. This is an enum that lists out four notification types that we have access to for toastr.js.

Notification. This is a class that holds our properties that we can modify for a notification.

Back to the layout page
Adding this snippet to your _Layout.cshtml page will handle the rendering of any pending notifications. I added mine at the very bottom.

@Html.Raw(NotificationsHelper.RenderNotifications())

Expanding this example
Toastr.js can take in a variety of information for a notification. Use this example as a base and continue onward with their sample docs here (Toastr.js).

Try building some custom html notifications, storing and rendering that using this guide as a base.

Discussion (9)

Collapse
jewishspiderweb profile image
Moshe Margareten

I followed all your instructions and when I launch my site 0 notifications appear...
Please help me.
Thanks!
Moshe

Collapse
charkinsdevelopment profile image
Cory Harkins Author

I'm sorry to here that. Do you have a hit hub where I can take a look? I'd love to help

Collapse
jewishspiderweb profile image
Moshe Margareten

Thanks so much, for your article and for your reply to my comment!
I do have a GitHub, but first maybe you can point me to where my problem is stuck, for example, you could say "Did you put your jquery before others in the BundleConfig?".

Thanks again!
All the best,
Moshe

Thread Thread
charkinsdevelopment profile image
Cory Harkins Author

To be fair I can throw out suggestions all day long; but I don't know what your setup looks like and I'd rather not guess at your setup.

But since you asked:

Did you put jquery before anything else required in your bundle config?
Did you install the toastr.js nuget and add those files to the bundle config?
Are you showing any kind of errors in your console when running the project?
Did you modify the layout.cshtml file and are you writing your notifications to session?
Did you deserialize the session string value correctly?
Is something outside of this project clearing out your session value before it gets to where it is parsed for the notifications?

Thread Thread
jewishspiderweb profile image
Moshe Margareten

I checked all your questions, and it seems that I did everything correctly.
I'll read again your post, and I'll do it again from scratch, as it can be I skipped a step etc. and if it won't work I'll contact you.
Thanks!
All the best,
Moshe

Thread Thread
jewishspiderweb profile image
Moshe Margareten

I tried it again, and it's working.

Thread Thread
jewishspiderweb profile image
Moshe Margareten

I tried again and it's working!
Thanks for the post and for helping me.
All the best!
Moshe

Collapse
cmiranda profile image
Cris 👨🏻‍💻

Please post the content here instead of sharing a medium link 😕

Collapse
charkinsdevelopment profile image
Cory Harkins Author

Cris,

Thanks for the feedback! I've updated the post with a link to my original article; and moved the content here. Happy to do so.

Best,

-Cory