我試圖讀取一些數據在JSON中,反序列化它,並將它顯示在我的視圖中的DropDownList中。數據看起來像這樣:ASP.NET MVC -JsonConvert.DeserializeObject到Html.DropDownList
{
"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,
...
}
]
}
]
}
我已經嘗試了一些不起作用的不同的東西。現在,我有我的控制器:
var data = Newtonsoft.Json.Linq.JObject.Parse(productsJson.ToString())["products"];
SelectList productsData = JsonConvert.DeserializeObject<SelectList>(data.ToString());
ViewData["products"] = productsData;
並在視圖:
@Html.DropDownList("ProductList", (SelectList)ViewData["products"])
目前,我發現了以下錯誤:
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.
幫助表示讚賞。
什麼文字,你試圖把在選擇列表中?我沒有看到任何好的候選人。 – McGarnagle