Search code examples
c#jqueryasp.net-mvcmodel-view-controllerviewbag

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 ?


Solution

  • You need to write it this way:

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