MVC實現下拉框聯動的執行個體詳解

來源:互聯網
上載者:User
這篇文章主要為大家詳細介紹了MVC實現下拉框聯動效果,具有一定的參考價值,感興趣的小夥伴們可以參考一下

下拉框聯動效果,我們以部門--職位為例,選擇部門時,關聯到該部門的職位.下拉框的寫法就不多說了,詳細請參照前文.

視圖:

其中,dept是部門的屬性,deptlist是部門下拉框的屬性,job是職位的屬性,joblist是職位下拉框的屬性,下拉框綁定請參照前文

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

當部門變動的時候,職位也相應改變:


//根據城市擷取酒店 $("#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"]("請選擇部門");    }   }); });

執行js裡的URL,這個程式寫在控制器裡:


  [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);  }
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.