ASP.NET MVC: How to pass Dictionary<string, string> from controller to jquery for use?

1.5k Views Asked by At

As I am quite green for ASP.NET MVC so maybe my question is very simple for many experienced users.

I am now implementing a web-based platform and I am trying to pass some data in Dictionary from Controller to View using ViewBag.

However, I am not able to use those ViewBag in jQuery, when I "alert" them, they become a series of [Object, Object].

So I would like to ask how I can retrieve those data in jQuery?

Thanks.

3

There are 3 best solutions below

2
On BEST ANSWER

You can serialize a .NET dictionary to Json. The Json.NET library has first class support for this

0
On

You could use jQuery Ajax to get this data from an action that returns the data in a JsonResult. While the other two approaches should work this approach does not require to load the whole bunch of data with the page and make your UI more responsive through Ajax.

0
On

just do a loop to go through dictionary in your view to bind to your controls. However, try to limit your business logic in views and keep in controller .

Or I am misunderstanding your question. Any specific example would help.