asp.net mvc - How mvc framework parse objects to JavaScript object? -


i pass data(refernce type or value type) in action function using viewbag or viewdata.

the passed data(in viewdata or viewbag) assigned javascript variable on client side:

var stuff = @viewbag.somedata; 

my question type of variable stuff be?is javascript object?or string?or maybe string in json format?

thank in advance.

what type of variable stuff be?

javascript has types now? yes does, don't think question want answered. furthermore question lacks lots of detail exactly want do. data come from? want it? why want assign javascript variable?

anyway, this:

var stuff = @viewbag.somedata; 

will work if @viewbag.somedata contains number (depending on culture though) or object .tostring() representation valid javascript.

razor has no notion of javascript. values print html-encoded. if viewbag.somedata contains number, html go this:

var stuff = 42; 

when it's decimal , culture uses comma that:

var stuff = 4,2; 

if contains string, it'll this:

var stuff = hello, world!; 

and if magic contains valid javascript object initializer:

var stuff = { foo: bar }; 

but usually, it'll print type name reference type:

var stuff = system.collections.generic.list`1[string]; 

almost examples above won't work. you'll want use quotes:

var stuff = '@viewbag.somedata'; 

but still data html-encoded, if want convert c# reference type json , assign javascript variable, explained in how write unencoded json view using razor?:

var stuff = @html.raw(json.encode(model.potentialattendees)); 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -