C# 把数据字典表的数据自动生成为 js 键值对形式

来源:互联网 发布:unity3d 联网 编辑:程序博客网 时间:2024/04/29 22:59
 /// <summary>
        /// 生成数字字典Js文件
        /// </summary>
        /// <returns></returns>
        public ActionResult GenerateDictJs()
        {
            var path = Path.Combine(HttpRuntime.AppDomainAppPath, @"KScripts\_wisdomsite\dictionary.js");
            if (System.IO.File.Exists(path))
            {
                System.IO.File.Delete(path);
            }
            using (var tt = new RealNameSystemEntities())
            {
                var ts = tt.sys_dict_type.Where(p => p.is_selectable == 0).ToDictionary(p => p.type_code, p => p.type_name);
                using (StreamWriter sw = System.IO.File.AppendText(path))
                {
                    sw.WriteLine("window.dictTransfer = {");
                    foreach (var t in ts)
                    {
                        sw.WriteLine("    /** {0} **/", t.Value);
                        sw.WriteLine("    {0}: [", t.Key);
                        foreach (var item in tt.sys_dict_item.Where(p => p.type_code == t.Key && p.del_flag == 0).ToList())
                        {
                            sw.WriteLine("    {{ text: '{0}', value: '{1}' }},", item.name, item.code);
                        }
                        sw.WriteLine("    ],");
                    }
                    sw.WriteLine("}");
                    sw.Flush();
                }
            }
            return Json("生成成功", JsonRequestBehavior.AllowGet);

        }



///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

window.dictTransfer = {
  
    /** 是否 **/
    SYS_YES_NO: [
    { text: '是', value: 'YES' },
    { text: '否', value: 'NO' },
    ]

    
}

阅读全文
0 0