Home >Backend Development >C#.Net Tutorial >Detailed explanation of examples of MVC implementing drop-down box linkage
This article mainly introduces MVC to realize the linkage effect of drop-down box in detail. It has certain reference value. Interested friends can refer to
The linkage effect of drop-down box. We use departments-- Take the position as an example. When selecting a department, the position associated with the department is linked. I won’t go into detail about how to write the drop-down box. Please refer to the previous article for details.
View:
Among them, dept is the attribute of the department, deptlist is the attribute of the department drop-down box, job is the attribute of the position, joblist is the attribute of the position drop-down box, please refer to the previous article for binding the drop-down box
@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>
When the department changes, the position also changes accordingly:
//根据城市获取酒店 $("#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"]("请选择部门"); } }); });
Execute the URL in js. This program is written in the controller:
[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); }
The above is the detailed content of Detailed explanation of examples of MVC implementing drop-down box linkage. For more information, please follow other related articles on the PHP Chinese website!