easyui-combobox 传递参数到后台获取json来绑定选项

  html-javascript:

  $(function () {
        $("#cmbRangeType_add").combobox({
            onChange: function (n, o) {
                var RangeType = n;//当前选择的[范围类型]             
                $("#cmdRangeValue_add").combobox("clear");//清空[范围值]选项
                $.getJSON("/Area/Controller/GetJson",
                    { RangeType: RangeType },
                    function (json) {
                        alert(json)
                        $("#cmdRangeValue_add").combobox({
                            data : json,//获取要显示的json数据  
                            valueField: 'id',
                            textField: 'text',
                        });
                });
            }
        });
    });

mvc-Controller:

  public ActionResult GetJson(string Type)
        {
            if (string.IsNullOrEmpty(Type) || string.IsNullOrWhiteSpace(Type)) return Content("");
  
            string json = JobViewRangeService.GetRangeValueJson(Type);
            return Content(json);
        }
JSON:
 public string SubjectJson()
        {
            //[{"id":1,"text":"text1"},{"id":2,"text":"text2"}....]
            var subject = GetSubject();
            if (subject != null && subject.Any())
            {
                string jsonData = "[";
                subject.ForEach(b =>
                {
                    jsonData += "{";
                    jsonData += "\"id\":\"" + b.SubjectID + "\",";
                    jsonData += "\"text\":\"" + b.SubjectName + "\"";
                    jsonData += "}";
                    jsonData += ",";
                });
                jsonData = jsonData.Substring(0, jsonData.Length - 1);//去掉末尾的 , 逗号
                jsonData += "]";
                return jsonData;
            }
            return string.Empty;
        }
        public List GetSubject()
        {
            string sql = @" SELECT [SubjectID],[SubjectName] FROM [T_Subject] WHERE IsValid=1 ORDER BY Sort ASC";
            DataTable dt = DbHelperSQL.QueryDataTable(sql);
            if (dt == null) return null;

            return dt.AsEnumerable().Select(n => new Subject
            {
                SubjectID = n.Field("SubjectID"),
                SubjectName = n.Field("SubjectName"),
            }).ToList();
        }






你可能感兴趣的:(html,jquery,ASP.NET代码,JS代码)