Toastr Notifications in Microsoft.AspNetCore.Mvc

11,861

Solution 1

For anyone who is stuck like me and in search of solution, I ended up using NToastNotify library after updating my packages to its required versions and it is working perfectly fine.

Solution 2

Yes, you can show toast notification with simple ViewBag. Here is my code.

The following code will display a toast error while loading the page, data from ViewBag

@section Scripts
{
    <script>
        $(function(){
            var title = '@ViewBag.Title';
            var message = '@ViewBag.Message';
            toastr.error(message, title);
        });
    </script>
}

And you can write similar code when user clicks on a button and want to show toast notifications.

$("#ToastNotification").click(function(){
    var title = '@ViewBag.Title';
    var message = '@ViewBag.Message';
    toastr.info(message, title);
});

I have added the toast script and css in the layout page

Share:
11,861
Naila Akbar
Author by

Naila Akbar

As a bright ambitious person, I enjoy working in a fast paced, highly motivating position where I can assist others while challenging and expanding my knowledge and understanding of the task at hand. I want to utilize my skills and offer the chance for advancement as well as to gain additional skills and experience.

Updated on June 05, 2022

Comments

  • Naila Akbar
    Naila Akbar almost 2 years

    I'm using Toastr notification plugin in my dotnetCore razor application to display status messages. These notifications are working perfectly fine with all ajax calls and their responses (in javascript).

    But I need to configure these responses on simple razor actions;

    asp-action="ViewJobs" asp-controller="Job"
    

    Like if there's some loading issue or rights issue, I need to show error messages as toastr notifications. I did some RnD on it and found out different solutions but nothing worked out for me.

    I've tried MVC wrapper for Toastr that handles all kind of toastrs in controller but its specific for MVC and trying to add some files in App_Code that is not available in dotnetcore and furthermore its having @helper tag that is no more supported in dotnetcore. (Tell me if there's any other workaround to use this solution?)

    Then I tried this solution NToastNotify , but got stuck again, because it needs Microsoft.AspNetCore.Mvc (>= 1.1.2) while I'm using version 1.0.0 and I cannot update my version because then I need to update so many files in my project.

    So Is there any other solution to handle this situation?? Can't I fulfill my requirement with simple viewbag or tempdata ??

    Any kind of help will be appreciated.

  • Naila Akbar
    Naila Akbar almost 7 years
    But if i have 5,6 buttons on a page, I've to handle each click??
  • dnx
    dnx over 6 years
    Thanks for the solution :)