How to display Checkbox list based on Dropdown selection?

Go To StackoverFlow.com

0

Following is my code with the business logic. I get below json by calling the webservice and and I deserialized that to the object (as shown in my Entity). Now, I want to display Dropdown with the category selections and once the user select the category it should display checkbox lists of subcategories.

As per my below code the dropdown should have "Eat" & "Entertainment" options. And if the user select "Entertainment" then it should display "All", "Movie, & "Bowling" as the checkbox list. Can anyone please help me how to have this in 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"}
                      ]
                     },
                     }
          ]
    }

Entity:

 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;
      }
2012-04-04 04:16
by updev


2

Use the following code in view @model MyData

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

        }
2012-04-04 04:30
by Nayana Setty
Ads