Ann
Ann

Reputation: 17

toastr notifications are not working using tempdata attribute

I want to show few alert messages using toaster. from my controller, i have passed tempdata variable to view. I kept this variable in a div attribute in view. by using this attribute, am trying to display messages using toaster. The problem is , tempdata is binding with div attribute. but toasr script is not working. below is the view view.cshtml

    <input type="submit" value="Submit" formaction="Add_Employee" class="btn btn-primary" />
    <div id="msgs" data-message ="@TempData["msg"]"></div>
}

@section Scripts {
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
    <script>
        $(function () {
            $("#datepicker").datepicker();
        });
    </script>
    <script type="text/javascript">
        $(document).ready(function ()

        {
       if($("#msgs").attr("data-message") == "") 
          toastr.error('Server error!! Please try after some time');

        else if ($("#msgs").attr("data-message") == "1")
            toastr.success('Record added successfully');
        else if ($("#msgs").attr("data-message") == "0")
            toastr.warning('Record already exist');
        else
          toastr.error('Operation failed');

    });
    </script>
}

In the above data-message of id "msgs" got binded. for example data-messages="2". but not showing notifications. Thanks in advance

Upvotes: 0

Views: 443

Answers (1)

ikiK
ikiK

Reputation: 6532

You should look into JS if statements syntax. You are making conditions without code to execute... You can not just drop it wherever you want.

if (condition) {
  //  block of code to be executed if the condition is true
} 

Put your toastr inside.

Also like comment said make sure you are even including toastr library correctly. Call it on page load without conditions to see if it even work, then include it in if conditions properly, if your conditions even work.

All sorts of things might gone wrong here in code presented.

Upvotes: 1

Related Questions