MVC realizes the linkage effect of drop down box (single choice)

  • 2021-10-11 18:01:50
  • OfStack

Drop-down box linkage effect, we take department-position as an example, when selecting a department, associated with the position of the department. The writing of the drop-down box is not much said, please refer to the previous article for details.

Views:

Among them, dept is the attribute of department, deptlist is the attribute of department drop-down box, job is the attribute of position drop-down box, and joblist is the attribute of position drop-down box. Please refer to the previous section for binding of drop-down box


@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>

When the department changes, the position changes accordingly:


// Get hotels by city 
 $("#dept").change(function () {
  var url = rootUrl + "aaa/GetJobByDept";
   var dept = $(this).val(); // Get the value of the department 
   var job = $("#job");
   job.empty();  // Empty the value of the current position 
   // This sentence is very important because what we use is select2 Plug-in, if you don't use this plug-in, you can remove this sentence 
   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"](" Please select a department ");
    }
   });
 });

Execute URL in js. This program is written in the controller:


  [Description(" Obtain positions by department ")]
  [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);
  }


Related articles: