我正在尝试使用JsonConvert.DeserializeObject
使用json数据绑定asp:dropdownlist,它正在获取完整的json,但我想将其与json数据的内部数组绑定。提取的数据类型为{System.Collections.ListDictionaryInternal}
,绑定时抛出异常{"Data source is an invalid type. It must be either an IListSource, IEnumerable, or IDataSource."}
。
ddlCategoryMain
完全绑定,
ddlCategorySub
ddlCategoryMain_SelectedIndexChanged
的例外情况
这是json数据:
{
"Category": [
{
"MainCategory": "Biography",
"SubCategory": [
"Historical",
"Political",
"Military",
"Musician"
]
},
{
"MainCategory": "Business",
"SubCategory": [
"Self-Employed",
"Taxation",
"Personal Finance",
"Organisational Behaviour"
]
},
{
"MainCategory": "Computers & Internet",
"SubCategory": [
"Computer Programing",
"Web Design",
"Mobile Phones",
"Computer Hardware",
"Operating Systems"
]
}
]
}
这是DataEntity:
public class bookcategoryDE
{
public List<Category> Category { get; set; }
}
public class Category
{
public int CategoryID { get; set; }
public string MainCategory { get; set; }
public List<string> SubCategory { get; set; }
}
这是我的代码:
(ASPX):
<asp:DropDownList ID="ddlCategoryMain" runat="server" DataTextField="MainCategory" DataValueField="MainCategory"
AutoPostBack="true" OnSelectedIndexChanged="ddlCategoryMain_SelectedIndexChanged">
</asp:DropDownList>
<br />
<br />
<asp:DropDownList ID="ddlCategorySub" runat="server" DataTextField="SubCategory" DataValueField="SubCategory"></asp:DropDownList>
(CS):
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
var JsonBookCategory = ReadJson<bookcategoryDE>(Server.MapPath("~/Json/Category.json"));
ddlCategoryMain.DataSource = JsonBookCategory.Category;
ddlCategoryMain.DataBind();
}
}
public T ReadJson<T>(string JsonPath)
{
using (StreamReader r = new StreamReader(JsonPath))
{
T keys = JsonConvert.DeserializeObject<T>(r.ReadToEnd());
return keys;
}
}
protected void ddlCategoryMain_SelectedIndexChanged(object sender, EventArgs e)
{
var JsonBookCategory = ReadJson<bookcategoryDE>(Server.MapPath("~/Json/Category.json"));
ddlCategorySub.DataSource = JsonBookCategory;
ddlCategorySub.DataBind();
}
答案 0 :(得分:1)
protected void ddlCategoryMain_SelectedIndexChanged(object sender, EventArgs e)
{
var JsonBookCategory = ReadJson<bookcategoryDE>(Server.MapPath("~/Json/Category.json"));
// JsonBookCategory is not IListSource, IEnumerable, or IDataSource(like say exception)
//ddlCategorySub.DataSource = JsonBookCategory;
var selectedCategoryName = ddlCategoryMain.SelectedValue;
var selectedCategory = JsonBookCategory.Category.Where(c => c.MainCategory== selectedCategoryName).SingleOrDefult();
if(selectedCateogry != null)
{
ddlCategorySub.DataSource = selectedCategory.Category;
ddlCategorySub.DataBind();
}
}
也许你的下拉配置应该是......
<asp:DropDownList ID="ddlCategoryMain" runat="server" DataTextField="MainCategory" DataValueField="MainCategory"
AutoPostBack="true" OnSelectedIndexChanged="ddlCategoryMain_SelectedIndexChanged">
</asp:DropDownList>
<br />
<br />
<asp:DropDownList ID="ddlCategorySub" runat="server" DataTextField="MainCategory" DataValueField="MainCategory"></asp:DropDownList>