Detailed explanation of examples of MVC implementing drop-down box linkage

零下一度
Release: 2017-07-02 10:32:44
Original
2423 people have browsed it

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>
Copy after login

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(&#39;val&#39;, &#39;&#39;);
   $.ajax({
    cache: false,
    type: "GET",
    url: url,
    data: { "Dept": dept},
    success: function (data) {
     $.each(data, function (id, option) {

      job.append($(&#39;<option></option>&#39;).val(option.Id).html(option.Name));
     });
     job.trigger(&#39;change&#39;);
    },
    error: function (xhr, ajaxOptions, thrownError) {
     toastr["error"]("请选择部门");
    }
   });
 });
Copy after login

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=&#39;{0}&#39;", 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);
  }
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template