简体   繁体   中英

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

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.

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM