下拉框连动实际效果,大家以部门--职位为例子,挑选部门时,关系到该部门的职位.下拉框的书写就很少讲过,详尽请参照上文.

主视图:

在其中,dept是部门的属性,deptlist是部门下拉框的属性,job是职位的属性,joblist是职位下拉框的属性,下拉框关联请参照前文

@using (Html.BeginForm("aaai003sch", "aaa", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
    @Html.AntiForgeryToken()
    <div class="modal-body">
        <div class="form-horizontal">
               <div class="form-group">
                    @Html.LabelFor(m => m.dept, new { @class = "col-sm-2 control-label" })
                        <div class="col-sm-10">
                            @Html.DropDownListFor(model => model.dept, Model.deptlist, new { @class = "form-control select2 ", style = "width: 100%;" })
                            @Html.ValidationMessageFor(m => m.dept, "", new { @class = "text-danger" })
                       </div>
               </div>
               <div class="form-group">
                @   Html.LabelFor(m => m.job, new { @class = "col-sm-2 control-label" })
                       <div class="col-sm-10">
                            @Html.DropDownListFor(model => model.job, Model.joblist, new { @class = "form-control select2 page-select2-area", style = "width: 100%;" })
                            @Html.ValidationMessageFor(m => m.job, "", new { @class = "text-danger" })
                       </div>
               </div>
        </div>
    </div>
</div>

当部门变化的情况下,职位也相对更改:

    //依据大城市获得酒店餐厅
    $("#dept").change(function () {
        var url = rootUrl   "aaa/GetJobByDept";
            var dept = $(this).val();   //获得部门的值
            var job = $("#job");
            job.empty();     //清除当今职位的值
            //这句话很重要,因大家用的是select2软件,若没有用这一软件能够除掉这句话
            job.select2('val', '');
            $.ajax({
                cache: false,
                type: "GET",
                url: url,
                data: { "Dept": dept},
                success: function (data) {
                    $.each(data, function (id, option) {

                        job.append($('<option></option>').val(option.Id).html(option.Name));
                    });
                    job.trigger('change');
                },
                error: function (xhr, ajaxOptions, thrownError) {
                    toastr["error"]("挑选部门");
                }
            });
    });

实行js里的URL,这一程序写在控制板里:

        [Description("依据部门获得职位")]
        [AcceptVerbs(HttpVerbs.Get)]
        [LoginAllowView]
        public ActionResult GetJobByDept(string dept)
        {
            if (String.IsNullOrEmpty(dept))
            {
                throw new ArgumentNullException("dept");
            }
            StringBuilder sb = new StringBuilder();
            sb = new StringBuilder();
            sb.Append(" SELECT jobid,jobname ");
            sb.Append(" FROM job_file ");
            sb.Append(" LEFT JOIN dept_file ON jobdept = deptid ");
            sb.AppendFormat(" WHERE deptid='{0}'", dept);
            DataTable dt = sqlHelper.getData(sb.ToString());
            var result = dt.AsEnumerable().Select(row => new Item
            {
                Name = Utils.ObjToStr(row["jobname"]),
                Id = Utils.ObjToInt(row["jobid"], 0)
            }).ToList();
            return Json(result, JsonRequestBehavior.AllowGet);
        }