Beispiel #1
0
        public static SelectOption Create(object instance, string valueProp = "Id", string textProp = "Name")
        {
            Dictionary <string, object> dic = Json.ToObject <Dictionary <string, object> >(Json.ToJson(instance));
            SelectOption option             = new SelectOption();

            if (dic.Keys.Contains(valueProp) && dic.Keys.Contains(textProp))
            {
                option.Value = dic[valueProp] == null ? null : dic[valueProp].ToString();
                option.Text  = dic[textProp] == null ? null : dic[textProp].ToString();
            }
            else
            {
                if (option.Value == null)
                {
                    int i = 0;
                    foreach (var k in dic.Keys)//获取第2个
                    {
                        if (i == 0)
                        {
                            option.Value = dic[k] == null ? null : dic[k].ToString();
                        }
                        if (i == 1)
                        {
                            option.Text = dic[k] == null ? null : dic[k].ToString();
                        }
                        i++;
                    }
                }
            }
            return(option);
        }
Beispiel #2
0
        public static List <SelectOption> CreateList <T>(IEnumerable <T> instanceList, string valueProp = "Id", string textProp = "Name")
        {
            List <SelectOption> options = new List <SelectOption>();

            foreach (var instance in instanceList)
            {
                SelectOption option = SelectOption.Create(instance, valueProp, textProp);
                options.Add(option);
            }

            return(options);
        }