Steven Spielberg
Steven Spielberg

Reputation:

how to define dynamic variable in javascript

when i define in javascript

var whoami = @ViewBag.myname

it is not work or render they render

var whoami = ;

i am trying it @(ViewBag.myname) // not worked too.

are their any way to do this in raor MVC 3

Upvotes: 1

Views: 440

Answers (1)

Don
Don

Reputation: 17606

Is @ViewBag.myname empty?

Enclose the variable in quotes, so to have a correct javascript string:

var whoami = "@ViewBag.myname";

also ensure that myname doesn't contain quotes too (or escape them).

Upvotes: 2

Related Questions