asp.net mvc - How to access item data in Javascript, if my Model is a QueryTable in MVC3? -



asp.net mvc - How to access item data in Javascript, if my Model is a QueryTable in MVC3? -

i new mvc. found site helpful. i've been struggling model info accessing javascript. here question.

i know if controller homecoming single item, can utilize <%: model.name%> in <script> section.

but case there more items in model, <%foreach (var item in model) %> not work in <script> works in other part of view.

can shed me lite on this?

also recommend book on mvc, , razor?

it helpful know more model class. likely, have class kind of collection:

public class collegecourse { public string professor {get;set;}; public list<student> students {get;set;}; }

you can loop through collection in server-side code this:

<% foreach (var pupil in model.students) %>

as far javascript... illustration gave illustration of how loop through list<t> , build html on server side. time page loads in browser, model info long gone.

to interact model info javascript, need take strategy getting info browser. few ideas:

transform info json on when page loads, write out in <script> tags. embed info in html tags, perhaps using data- attributes. can utilize jquery retrieve values. add action controller returns info need json. then, after page loads, can utilize jquery ajax phone call retrieve model info , whatever magic plan it.

the first 2 options aren't considered practices. i'd recommend ajax approach. here's quick example shows how might able this. it's not difficult, , mvc3 has built-in helpers create setting ajax phone call simpler.

if you're comforable asp.net, professional asp.net mvc3 first-class book head around mvc , razor.

javascript asp.net-mvc asp.net-mvc-3

Comments

Popular posts from this blog

delphi - blogger via idHTTP : error 400 bad request -

c++ - compiler errors when initializing EXPECT_CALL with function which has program_options::variables_map as parameter -

How do I check if an insert was successful with MySQLdb in Python? -