Use Toastr messages from codebehind

No comments
I have been using the excellent toastr library for my clientside messages now for a while.

Today I needed to send a message from codebehind to the client and I wanted to keep using toastr.

As I prefer to solve things only once I wanted this to be reusable through my app.
Something like:

var msg= new Message(Level.Error,"This is the text", "This is the title"));
viewmodel.Messages.Add(msg);

and then have this show up the nice toastr way:





(See demo over at GitHub for all the options.)


So to have a reusable way to send messages to my users with Toastr I came up with this model:


First I define a model for messages


Next I create a ViewModel class and inherit from it:



In the controller I can now easily add messages:




Add finally make them pop up in the view:






I can now add multiple messages with different levels, all from codebehind:



No comments :

Post a Comment