asp.net - Using ViewData in Javascript code -


i'm creating viewdata in asp.net controller this:

var elements = db.requests.groupby(user => user.submituser)                    .select(slt => new { userscount = slt.key, countreq = slt.count() }); 

this have example

userscount      count  user1           3  user2           3  user3           3 

then in javascript want create array userscount , count, example:

var arrayusers = viewdata.userscount; var arraycounter = viewdata.countreq; 

i tried several solutions , can't it. what's best solution?

controller:

 public actionresult statistics()         {             var elements = db.requests.groupby(user => user.submituser)                             .select(slt => new { userscount = slt.key, countreq = slt.count() });               viewdata["userscounter"] = elements;              return view();         } 

javascript in view

<script>     var check = @viewdata["userscounter"]**;**error  ....  } 

to use viewdata in javascript tag prefix viewdata @ symbol

var check = @viewdata["array"] 

in controller

viewdata["array"] = elements; 

why not set variable model since looks you're setting that

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

then can whatever

var blur = check.username 

Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -