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?
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";
});
Try to enclose the variables in ""
like this:
$("document").ready(function(){
@{
var cx = Json.Encode(ViewBag.x);
var cy = Json.Encode(ViewBag.y);
}
var x = "@cx";
var y = "@cy";
});
You may also want to check ASP.NET MVC 3: Razor’s @: and syntax
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With