How to fill javascript variables with c# ones?

c#, javascript, razor

Solution

You must enclose the js variables unless they are numeric or boolean

$("document").ready(function(){
    @{
        var cx = Json.Encode(ViewBag.x);
        var cy = Json.Encode(ViewBag.y);
    }
    var x = "@cx";
    var y = "@cy";
});

Problem

I have these lines in my view.cshtml: ``` $("document").ready(function(){ @{ var cx = Json.Encode(ViewBag.x); var cy = Json.Encode(ViewBag.y); } var x = @cx; var y = @cy; }); ``` But now there is a red line under `;` in javascript codes and the error is `Syntax error`. What is the problem?

Original source

Related problems