以下是我的业务逻辑代码。我通过调用webservice获得了json,并将其反序列化为对象(如我的实体所示)。现在,我想显示Dropdown和类别选择,一旦用户选择类别,它应该显示子类别的复选框列表。
根据我的下面代码,下拉列表应该有“Eat”& “娱乐”选项。如果用户选择“娱乐”,那么它应该显示“全部”,“电影”和“保龄球”作为复选框列表。任何人都可以帮助我如何在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;
}
答案 0 :(得分:2)
在视图中使用以下代码 @model MyData
@foreach (Category item in Model.Categories)
{
<input type="checkbox" name="Categories" value="@item.categoryId" id="@item.categoryId"/>
}