How to put viewbag value as a variable in javascript/ jquery?

18,623

Solution 1

You need to write it this way:

var ThematicId = parseInt('@ViewBag.thematicid');
var ThematicName =  '@ViewBag.Name'; 

Solution 2

var ThematicId = @(ViewBag.thematicid); //For int
var ThematicName =  '@ViewBag.Name';   //For string
Share:
18,623
Julien698
Author by

Julien698

Updated on June 04, 2022

Comments

  • Julien698
    Julien698 almost 2 years

    I try to put two variable in JQuery which came from viewbags :

            $("#btnAdd").click(function () {
            var url = dev + "/Legacy/PutContentInThematic";
            var GroupingId = $("#GroupingId_Dialog").val();            
            var Title = $("#Title_Dialog").val();
            var Synopsis = $("#Description_Dialog").val();
            var image = $("#Image_Dialog").val();
            var ThematicId = @ViewBag.thematicid //Here i can't put ';'
            var ThematicName =  @ViewBag.Name
    
            $.ajax({
                url: url,
                cache: false,
                type: 'POST',
                data: {
                    GroupingId: GroupingId,
                    ThematicId: ThematicId,
                    Title: Title,
                    Synopsis: Synopsis,
                    Image: image,
                    ThematicName: ThematicName
                }
            });
        });
    

    The problem concerns the two last variables "ThematicId" And "ThematicName", it is impossible to put ';' at the end, so the second variable ThematicName don't work.

    I try to put this variable out of the function, but i doesn't work anymore. Have you and idea to fix it or an other solution for this Ajax call ?