Ejemplo n.º 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);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 3
0
        public static SelectOption Create(object instance, string valueProp = "Id", string textProp = "Name")
        {
            object value = instance.GetPropertyValue(valueProp);
            object text  = instance.GetPropertyValue(textProp);

            SelectOption option = new SelectOption();

            option.Value = value == null ? null : value.ToString();
            option.Text  = text == null ? null : text.ToString();

            return(option);
        }