C# &引用;{";数据源是无效类型。它必须是IListSource、IEnumerable或IDataSource。";}";装订时
我正在尝试使用C# &引用;{";数据源是无效类型。它必须是IListSource、IEnumerable或IDataSource。";}";装订时,c#,json,C#,Json,我正在尝试使用JsonConvert.DeserializeObject将asp:dropdownlist与json数据绑定,它正在获取完整的json,但我想将其与json数据的内部数组绑定。获取的数据的类型为{System.Collections.ListDictionaryInternal},并引发异常{“数据源是无效的类型。绑定时它必须是IListSource、IEnumerable或IDataSource。”} ddlCategoryMain完全绑定 ddlCategoryMain\u
JsonConvert.DeserializeObject
将asp:dropdownlist与json数据绑定,它正在获取完整的json,但我想将其与json数据的内部数组绑定。获取的数据的类型为{System.Collections.ListDictionaryInternal}
,并引发异常{“数据源是无效的类型。绑定时它必须是IListSource、IEnumerable或IDataSource。”}
ddlCategoryMain
完全绑定
ddlCategoryMain\u SelectedIndexChanged上绑定ddlCategorySub
时出现异常
以下是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; }
}
公共类图书分类
{
公共列表类别{get;set;}
}
公共类类别
{
public int CategoryID{get;set;}
公共字符串MainCategory{get;set;}
公共列表子类别{get;set;}
}
这是我的密码:
(.aspx):
(.cs):
受保护的无效页面加载(对象发送方,事件参数e)
{
如果(!Page.IsPostBack)
{
var JsonBookCategory=ReadJson(Server.MapPath(“~/Json/Category.Json”);
ddlCategoryMain.DataSource=JsonBookCategory.Category;
ddlCategoryMain.DataBind();
}
}
public T ReadJson(字符串JsonPath)
{
使用(StreamReader r=newstreamreader(JsonPath))
{
T keys=JsonConvert.DeserializeObject(r.ReadToEnd());
返回键;
}
}
受保护的无效ddlCategoryMain\u SelectedIndexChanged(对象发送方,事件参数e)
{
var JsonBookCategory=ReadJson(Server.MapPath(“~/Json/Category.Json”);
ddlCategorySub.DataSource=JsonBookCategory;
ddlCategorySub.DataBind();
}
受保护的无效ddlCategoryMain\u SelectedIndexChanged(对象发送方,事件参数e)
{
var JsonBookCategory=ReadJson(Server.MapPath(“~/Json/Category.Json”);
//JsonBookCategory不是IListSource、IEnumerable或IDataSource(比如说exception)
//ddlCategorySub.DataSource=JsonBookCategory;
var selectedCategoryName=ddlCategoryMain.SelectedValue;
var selectedCategory=JsonBookCategory.Category.Where(c=>c.maincegory==selectedCategoryName.singleOrdFeature();
如果(SelectedCateLogy!=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>
No,我想在“main category”索引更改中将其与“sub category”绑定。使用此命令:“var selectedCategory JsonBookCategory.category.Where(c=>c.maincegory==selectedCategoryName)。singleOrdfilet();”这是相同的异常我认为有构建错误,并运行旧代码。。我更新了postReadJson
方法返回的是什么?
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();
}
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>