使用MVC框架怎么實(shí)現(xiàn)一個(gè)下拉框聯(lián)動(dòng)效果?相信很多沒有經(jīng)驗(yàn)的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。
網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)!專注于網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、微信小程序開發(fā)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了禹會(huì)免費(fèi)建站歡迎大家使用!@using (Html.BeginForm("aaai003sch", "aaa", FormMethod.Post, new { @class = "form-horizontal", role = "form" })) { @Html.AntiForgeryToken()@Html.LabelFor(m => m.dept, new { @class = "col-sm-2 control-label" })@Html.DropDownListFor(model => model.dept, Model.deptlist, new { @class = "form-control select2 ", style = "width: 100%;" }) @Html.ValidationMessageFor(m => m.dept, "", new { @class = "text-danger" })@ Html.LabelFor(m => m.job, new { @class = "col-sm-2 control-label" })@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" })
當(dāng)部門變動(dòng)的時(shí)候,職位也相應(yīng)改變:
//根據(jù)城市獲取酒店 $("#dept").change(function () { var url = rootUrl + "aaa/GetJobByDept"; var dept = $(this).val(); //獲取部門的值 var job = $("#job"); job.empty(); //清空當(dāng)前職位的值 //這句很重要,因我們用的是select2插件,若沒有用這個(gè)插件可以去掉這句 job.select2('val', ''); $.ajax({ cache: false, type: "GET", url: url, data: { "Dept": dept}, success: function (data) { $.each(data, function (id, option) { job.append($('').val(option.Id).html(option.Name)); }); job.trigger('change'); }, error: function (xhr, ajaxOptions, thrownError) { toastr["error"]("請選擇部門"); } }); });
執(zhí)行js里的URL,這個(gè)程式寫在控制器里:
[Description("根據(jù)部門獲取職位")] [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); }
看完上述內(nèi)容,你們掌握使用MVC框架怎么實(shí)現(xiàn)一個(gè)下拉框聯(lián)動(dòng)效果的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!