级联变动
public JsonResult getDepartmentJson(int CompanyId = 0)
        
{
            List<SelectListItem> SelectItems = new 
List<SelectListItem>();
            IEnumerable<Department> SerieslistsJieGuo = 
db.department.Where(o => o.CompanyId == CompanyId).ToList();
            
foreach (Department br in SerieslistsJieGuo)
            {
                
SelectItems.Add(new SelectListItem { Text = br.DepartmentName, Value = 
br.Id.ToString() });
            }
            return Json(SelectItems, 
JsonRequestBehavior.AllowGet);
        }
  //查询出所有公司
            ViewBag.Company = new 
SelectList(db.company.OrderBy(o=>o.Id),"Id","CompanyName");
            
//默认显示Id排序第一个公司的部门
            int firstCompanyId = db.company.OrderBy(o 
=> o.Id).FirstOrDefault().Id;
            ViewBag.Department = new 
SelectList(db.department.Where(o=>o.CompanyId==firstCompanyId), "Id", 
"DepartmentName");
//前台页面
<script language="javascript" type="text/javascript">
    $(document).ready(function () {
        $("#Company").change(function 
() { getSeriesSelectList() });
    });
    function getSeriesSelectList() {
        
$("#Department").empty();
        if ($("#Company").val() != "") 
{
            var url = "/Home/getDepartmentJson?CompanyId=" + 
$("#Company").val();
            $.getJSON(url, function (data) 
{
                $.each(data, function (i, item) {
                    
$("<option></option>").val(item["Value"]).text(item["Text"]).appendTo("#Department");
                
});
            });
        }
    
}
</script>
@Html.DropDownList("Company")
@Html.DropDownList("Department")
 
                    
                     
                    
                 
                    
                
 
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号