views:

26

answers:

2

Hi,

I am quite new in ASP.net MVC model and having a bit problem here (absolutely newbie).

My problem is i cannot access the ViewData via client-side java script to render a table using the data from my server side.

I having a main menu at the top consist of several link button, after the user click on it, it will change the sub menu on the right side. The menu item is dynamic where is queue from my server database.

any suggestion will be appreciated.

Thanks, in advance.

A: 

You need to write it out from within server tags like:

<%= ViewData["whatever"] %>
kekekela
yes, i understand.
Lim Zhi Wei
but 1 thing is i put it in<script type="text/javascript"> var menuList = <%=ViewData["MenuList"]%></script>in my master pagebut i cannot access the viewdata in the client side scripting code
Lim Zhi Wei
+1  A: 

ViewData["something"] contains some server side object which cannot be used directly by javascript unless some simple type such as integer, string, ... You could JSON serialize it:

<script type="text/javascript">
    var menuList = <%= new JavaScriptSerializer().Serialize(ViewData["MenuList"]) %>;
    // TODO: use the menuList javascript variable
</script>

Also I would recommend you using strongly typed views with view models instead of the ViewData hashtable.

Darin Dimitrov