samedi 11 avril 2020

make a generic js file to be used across all views

So there is a script I wrote in each view to show notification status according to a specific result. I want to make it in a single js file and then just call it in each view. the problem is @TempData can't be read in the js file or to be clear outside the .cshtml files. So what should I do to avoid this redundancy?

<script>

     $(function () {

     var msg = "@(TempData["error"] as string)";
        if (msg !== "") {

            toastr.error('@TempData["error"]');
            toastr.options = {

                "positionClass": "toast-bottom-right"

            }
        }

    });

     $(function(){
     var msg = "@(TempData["success"] as string)";
        if (msg !== "") {

            toastr.success('@TempData["success"]');
            toastr.options = {

                "positionClass": "toast-bottom-right"

            }
        }

    });

    $(function(){
     var msg = "@(TempData["warning"] as string)";
        if (msg !== "") {

            toastr.warning('@TempData["warning"]');
            toastr.options = {

                "positionClass": "toast-bottom-right"

            }
        }
    });

</script>

Aucun commentaire:

Enregistrer un commentaire