mvc2中的二级联动(非用户控件)

发布时间:2013-10-30 05:09:08来源:阅读(835)

    我要达到的效果是在前面的下拉列表中选择机构,在后面的下拉列表中出现对应的部门。在部门表Dept中包含OrgID,但不是外键。在mvc中一般不设有外键。

    helper中代码:

    /// /// 根据机构id读取部门 /// /// /// public IList GetDeptByOrgID(int id) { using (UUMContext uc = new UUMContext()) { var dept = from a in uc.Depts select a; if (id >= 0) { dept = dept.Where(a => a.OrgID == id); } IList d = dept.ToList(); return d; } }

    controller中代码:

    public ActionResult GetDeptByOrg(string orgId) { int orgid = 0; try { orgid = Convert.ToInt32(orgId); } catch { return Json(""); } return Json(GetDeptByOrgId(orgid, 0), JsonRequestBehavior.AllowGet); } /// /// 根据机构id获取部门 /// /// /// /// public SelectList GetDeptByOrgId(int orgId, int defVal) { IList items = deptHelper.GetDeptByOrgID(orgId); SelectList lists = new SelectList(items, "DeptId", "DeptName", defVal); return lists; }

    View中代码:

    若要查看id,可查看源文件中的id值,保证jquery中的id值与之相同。

关键字