1. 程式人生 > >easyui-combobox 傳遞引數到後臺獲取json來繫結選項

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<Subject> 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<int>("SubjectID"),
                SubjectName = n.Field<string>("SubjectName"),
            }).ToList();
        }