2012-04-04 212 views
0

以下是我的代碼與業務邏輯。我得到下面的JSON調用web服務,並與我反序列化,要(在我的實體所示)的對象。現在,我想與該類別選項顯示下拉一旦用戶選擇它應該顯示覆選框子類別列表中的類別。基於下拉選擇如何顯示覆選框列表?

按我下面的代碼的下拉應該有「吃」 &「娛樂」的選項。如果用戶選擇「娛樂」,那麼它應該顯示「全部」,「電影,&‘保齡球’的複選框列表任何人都可以請幫助我如何有這MVC3 & C#

JSON:?

{ 
    "Code":0, 
    "Status":"Done", 
    "Categories":[ 
       { 
       "ID":1, 
       "Name":"Eat", 
       "Subcategories":[ 
        {"Flag":false,"ID":100,"Name":"Food"}, 
        {"Flag":false,"ID":101,"Name":"Fast Food"},   
        {"Flag":false,"ID":102,"Name":"Other"} 
        ] 
        }, 
       { 
       "ID":2, 
       "Name":"Entertainment", 
       "Subcategories":[ 
         {"Flag":false,"ID":100,"Name":"All"},    
         {"Flag":false,"ID":101,"Name":"Movie"}, 
         {"Flag":false,"ID":102,"Name":"Bowling"} 
         ] 
        }, 
        } 
      ] 
    } 

實體:

public class MyData 
{ 
    public int Code { get; set; } 
    public string Status { get; set; } 
    public List<Category> Categories { get; set; } 
    } 

    public class Category 
    { 
     public string Name { get; set; } 
     public int ID { get; set; } 
     public List<Subcategory> Subcategories { get; set; } 
    } 

    public class Subcategory 
    { 
    public string Name { get; set; } 
    public int ID { get; set; } 
    public bool Flag { get; set; } 
    } 

ServiceCall:

public MyData GetAllCategories() 
    { 
      HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://host/data/categories/"); 

      request.ContentType = "application/json; charset=utf-8"; 
      request.Method = "GET"; 

      string returnJson = string.Empty; 

      using (WebResponse response = request.GetResponse()) 
      { 
       using (Stream stream = response.GetResponseStream()) 
       { 
        using (StreamReader reader = new StreamReader(stream)) 
        { 
         while (!reader.EndOfStream) 
         { 
          returnJson += reader.ReadLine(); 
         } 
        } 
       } 
      } 

      MyData deserializedMyData = JsonConvert.DeserializeObject<MyData>returnJson 


      return deserializedMyData; 
     } 

回答

2

使用以下代碼 @model MyData

@foreach (Category item in Model.Categories) 
     { 
<input type="checkbox" name="Categories" value="@item.categoryId" id="@item.categoryId"/> 

     } 
相關問題