您好,登錄后才能下訂單哦!
小編給大家分享一下MVC實(shí)現(xiàn)下拉框聯(lián)動(dòng)的方法,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
視圖:
其中,dept是部門的屬性,deptlist是部門下拉框的屬性,job是職位的屬性,joblist是職位下拉框的屬性,下拉框綁定請(qǐng)參照前文
@using (Html.BeginForm("aaai003sch", "aaa", FormMethod.Post, new { @class = "form-horizontal", role = "form" })) { @Html.AntiForgeryToken() <p class="modal-body"> <p class="form-horizontal"> <p class="form-group"> @Html.LabelFor(m => m.dept, new { @class = "col-sm-2 control-label" }) <p 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" }) </p> </p> <p class="form-group"> @ Html.LabelFor(m => m.job, new { @class = "col-sm-2 control-label" }) <p 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" }) </p> </p> </p> </p> </p>
當(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($('<option></option>').val(option.Id).html(option.Name)); }); job.trigger('change'); }, error: function (xhr, ajaxOptions, thrownError) { toastr["error"]("請(qǐng)選擇部門"); } }); });
執(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); }
以上是MVC實(shí)現(xiàn)下拉框聯(lián)動(dòng)的方法的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。