make a generic js file to be used across all views

asp.net-mvc c# design-patterns entity-framework-6

Question

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>
1
0
4/11/2020 10:16:13 PM

Popular Answer

TempData is a dictionary type which is designed to be used in the subsequent request. You can retain TempData values in a third consecutive request by TempData.Keep() enter image description here

In your case TempData is not suitable. As you are using .NET MVC framework so you may write something in your common Layout page like below before Renderbody(). Pass your message with ViewBag and it will show to all pages as all view will extend common layout view.

  <div class="col-md-12 sidebar-offcanvas" id="bodyContent" role="navigation">
            <br />
            <div class="customMessage text-center">
                @if (ViewBag.ErrorMessage != null)
                {
                    <div class="alert alert-danger">
                        <a class="close" data-dismiss="alert">×</a>
                        <strong>Error!</strong> @ViewBag.ErrorMessage
                    </div>
                }

                @if (ViewBag.SuccessMessage != null)
                {
                    <div class="alert alert-success">
                        <a class="close" data-dismiss="alert">×</a>
                        <strong>Success!</strong> @ViewBag.SuccessMessage
                    </div>
                }

                @if (ViewBag.InfoMessage != null)
                {
                    <div class="alert alert-info">
                        <a class="close" data-dismiss="alert">×</a>
                        <strong>Info! </strong>@ViewBag.InfoMessage
                    </div>
                }

                @if (ViewBag.WarningMessage != null)
                {
                    <div class="alert alert-warning">
                        <a class="close" data-dismiss="alert">×</a>
                        <strong>Warning! </strong>@ViewBag.WarningMessage
                    </div>
                }
            </div>
0
4/12/2020 7:53:53 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow