ASP.NET MVC - JsonConvert.DeserializeObject to Html.DropDownList

midnex

New Member
I'm trying to read some data in as Json, deserialize it, and display it in my view in a DropDownList. The data looks like this:\[code\]{ "products": [ { "body_html": "<p> ....</p>", "created_at": "2012-09-19T11:55:44-04:00", ... "tags": "Emotive, Flash Memory, MP3, Music", "variants": [ { "compare_at_price": null, "created_at": "2012-09-19T11:55:44-04:00", ... "inventory_quantity": 10 } ], "images": [ { "created_at": "2012-09-19T11:55:44-04:00", "id": 850703190, ... } ] } ]}\[/code\]I've tried a few different things which haven't worked. Right now I have in my controller:\[code\]var data = http://stackoverflow.com/questions/12614258/Newtonsoft.Json.Linq.JObject.Parse(productsJson.ToString())["products"];SelectList productsData = http://stackoverflow.com/questions/12614258/JsonConvert.DeserializeObject<SelectList>(data.ToString());ViewData["products"] = productsData;\[/code\]And in the view:\[code\]@Html.DropDownList("ProductList", (SelectList)ViewData["products"])\[/code\]Presently I'm getting the following error:\[quote\] Cannot create and populate list type System.Web.Mvc.SelectList. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.InvalidOperationException: Cannot create and populate list type System.Web.Mvc.SelectList.\[/quote\]Help is appreciated.
 
Back
Top