如何将viewbag值作为变量放在javascript / jquery中?

时间:2015-02-16 11:10:42

标签: c# jquery asp.net-mvc model-view-controller viewbag

我尝试将两个变量放在来自viewbags的JQuery中:

        $("#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
            }
        });
    });

问题涉及最后两个变量" ThematicId"并且" ThematicName",不可能把&#39 ;;'最后,所以第二个变量ThematicName不起作用。

我尝试将此变量放在函数之外,但我不再工作了。您是否有想法为此Ajax调用修复它或其他解决方案?

2 个答案:

答案 0 :(得分:8)

你需要这样写:

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

答案 1 :(得分:3)

var ThematicId = @(ViewBag.thematicid); //For int
var ThematicName =  '@ViewBag.Name';   //For string