页面2级分类

 1  public ActionResult Form(int? id) 
 2         {
 3             QueryType();
 4             EntryModel model = new EntryModel();
 5             if (id != null)
 6             {
 7                 model = business.Get(id.Value);
 8             }
 9             return View(model);
10         }
11 
12         private void QueryType()
13         {
14             EntryTypeRepository rep = new EntryTypeRepository();
15             var list = rep.QueryByParentId(0);
16             var select = new List<SelectListItem>();
17             foreach (var item in list)
18             {
19                 select.Add(new SelectListItem() { Text=item.Name,Value = item.Id.ToString()});
20             }
21             ViewBag.TypeList = select;
22         }
23 
24         public ActionResult GetSecondType(int id = 0)
25         {
26             var list = new List<EntryTypeModel>();
27             if (id != 0)
28             {
29                 EntryTypeRepository rep = new EntryTypeRepository();
30                 list = rep.QueryByParentId(id);
31             }
32             else{
33                 list = new List<EntryTypeModel>();
34             }
35             return Json(list, JsonRequestBehavior.AllowGet);
36         }
37 
38         [HttpPost]
39         public ActionResult Form(EntryModel model)
40         {
41             if (ModelState.IsValid)
42             {
43                 if (model.Id == 0)
44                 {
45                     business.Add(model);
46                 }
47                 else
48                 {
49                     business.Update(model.Id,model);
50                 }
51             }
52             return RedirectToAction("List");        
53         }
  <li>
    @Html.LabelFor(m => m.TypeId)
      @Html.DropDownList("Parent", ViewBag.TypeList as List<SelectListItem>, new { onchange = "GetTypeByParentID(this)" })
      子类:<select id="TypeId" name="TypeId" style="100px;">
    <option value="0">请选择子类</option>
  </select>
    @Html.ValidationMessageFor(m => m.TypeId)
 1 <script type="text/javascript">
 2     $(function () {
 3         GetTypeByParentID($("#Parent"));
 4     });
 5     function GetTypeByParentID(id) {
 6         $("#TypeId").empty();
 7         $.ajax({
 8             url: '/Entry/GetSecondType/' + $("#Parent").val(),
 9             type: "get",
10             datatype: "json",
11             success: function (data) {
12                 if (data.length == 0) {
13                     $("<option></option>")
14                     .val("0")
15                     .text("请选择子类")
16                     .appendTo($("#TypeId"));
17                 }
18                 $.each(data, function (i, item) {
19                     $("<option></option>")
20                     .val(item["Id"])
21                     .text(item["Name"])
22                     .appendTo($("#TypeId"));
23                 });
24             }
25         });
26     }
27 </script>
原文地址:https://www.cnblogs.com/isylar/p/3247345.html