2017-02-16 44 views
1

我想綁定asp:dropdownlist與json數據使用JsonConvert.DeserializeObject,它是獲取完整的json,但我想要綁定它與json數據的內部數組。獲取的數據類型爲{System.Collections.ListDictionaryInternal},並且綁定時拋出異常{"Data source is an invalid type. It must be either an IListSource, IEnumerable, or IDataSource."}「{」數據源是無效類型。它必須是IListSource,IEnumerable或IDataSource。「}」綁定

ddlCategoryMain是完全結合,

異常在ddlCategoryMain_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; } 
} 

這是我的代碼:

(的.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

正在返回什麼通過'ReadJson '方法? –

回答

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> 
+0

不,我想將它與「主類別」索引更改中的「子類別」綁定。 –

+0

與此:「var selectedCategory JsonBookCategory.Category.Where(c => c.MainCategory == selectedCategoryName).SingleOrDefult();」 它是一樣的異常 –

+0

我認爲有構建錯誤,並運行你的舊代碼..我更新後發佈 – levent

相關問題