//根据城市获取酒店
$("#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($('').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);
}