以下是我的业务逻辑代码。我通过调用 web 服务得到低于 json 的信息,并将其反序列化为对象(如我的实体中所示)。现在,我想显示带有类别选择的下拉菜单,一旦用户选择了类别,它应该显示子类别的复选框列表。
根据我下面的代码,下拉菜单应该有“吃”和“娱乐”选项。如果用户选择“娱乐”,那么它应该显示“全部”、“电影”和“保龄球”作为复选框列表。谁能帮助我如何在 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; }
}
服务电话:
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;
}