Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ?

like image 456
Julien698 Avatar asked Jun 24 '26 16:06

Julien698


1 Answers

You need to write it this way:

var ThematicId = parseInt('@ViewBag.thematicid');
var ThematicName =  '@ViewBag.Name'; 
like image 109
Ehsan Sajjad Avatar answered Jun 27 '26 07:06

Ehsan Sajjad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!