Search code examples
c#jqueryasp.net-mvcviewbag

How To Use ViewBag Data In jQuery


Here I have ViewBag in a controller which contains text i.e ViewBag.Msg = "No Sprint".
And I am trying to use ViewBag.Msg in Jquery to display modal.
Now, the problem is that the script is script never gets executed.

Below is my jQuery.

 <script>
    $(document).ready(function () {
        debugger;
        if (@ViewBag.Msg != null)
        {
            $("#myModal").modal();
        }
    });
</script>


Any help will be highly appreciated. Thank You.


Solution

  • Please use the below code.

    <script>
        $(document).ready(function () {
            debugger;
            var msg='@ViewBag.Msg';
            if (msg !== '')
            {
                $("#myModal").modal();
            }
        });
    </script>