1

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.

1
  • Show the code in the controller action and the jquery code from your view Commented Oct 20, 2011 at 4:10

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

2 Comments

The System.Web.Extensions.dll has a System.Web.Script.Serialization.JavaScriptSerializer that works too
@JeremyBell - Absolutely correct. Its actually a matter of preference. Good input
1

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.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.