private static string getSelectOptions(QueryItem queryItem, string selectedValue = null) { List <SelectListItem> listItems = new List <SelectListItem>(); if (queryItem.QueryPropertyType == QueryPropertyType.Enum) { listItems = SelectListHelper.GetEnumSelectListItems(queryItem.EntityType, selectedValue); } else { MethodInfo method = typeof(SelectListHelper).GetMethod("GetSelectListItems"); method = method.MakeGenericMethod(queryItem.EntityType); listItems = method.Invoke(null, new object[] { queryItem.ValueProperty, queryItem.TextProperty, selectedValue, null, null }) as List <SelectListItem>; } StringBuilder sb = new StringBuilder(); foreach (var item in listItems) { string s = string.Format("<option value=\"{0}\"{1}>{2}</option>", item.Value, item.Selected ? "selected=\"selected\"" : "", item.Text); sb.Append(s); } return(sb.ToString()); }
//构造函数 关联查询属性 public QueryItem(Type modelType, PropertyInfo propertyInfo, AssociatedQueryAttribute aqa) { bool hasSpecifiedValue = !(aqa.AssociatedPropertyName == null && aqa.DisplayName == null && aqa.TextPropertyName == null && aqa.ValuePropertyName == null); //QueryPropertyType this.QueryPropertyType = aqa.QueryPropertyType; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // aqa: QueryPropertyType AssociatedPropertyName DisplayName ValuePropertyName TextPropertyName // 无标签 Select null null null null // 有标签 Select null 指定 Clas.College.University.Id Clas.College.University.Name // 有标签 指定(非Select) Clas.College.University.Name 指定 null null ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // QueryItem: QueryPropertyType EntityType PropertyName DisplayName ValueProperty TextProperty // 无标签 Select Clas Clas.Id 获取 Id Name // 有标签 Select University Clas.College.University.Id 指定 Id Name // 有标签 指定(非Select) University Clas.College.University.Name 指定 null null ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //有[AssociatedQuery]标识,或者有[AssociatedQuery]标识且不带参数 if (!hasSpecifiedValue) { if (Config.AssociatedQueryAttributeAddedOnForeignKey) { PropertyInfo nvProperty = getNavigationPropertyByForeignKeyName(modelType, propertyInfo.Name); if (nvProperty == null) { throw new Exception("查询对话框异常:" + modelType.Name + "类中未找到与" + propertyInfo.Name + "属性相匹配的导航属性"); } this.EntityType = nvProperty.PropertyType; this.PropertyName = nvProperty.Name; } else { this.EntityType = propertyInfo.PropertyType; PropertyInfo fkProperty = getForeignKeyPropertyByNavigationPropertyTypeName(modelType, this.EntityType.Name); if (fkProperty == null) { throw new Exception("查询对话框异常:" + modelType.Name + "类中未找到与" + propertyInfo.Name + "属性相匹配的外键属性"); } this.PropertyName = propertyInfo.Name; } this.DisplayName = getPropertyDisplayName(propertyInfo); this.ValueProperty = SelectListHelper.GetSelectListValuePropertyInfo(this.EntityType); this.TextProperty = SelectListHelper.GetSelectListTextPropertyInfo(this.EntityType); this.PropertyName += "." + this.ValueProperty.Name; } //有[AssociatedQuery]标识且带参数 Select else if (this.QueryPropertyType == QueryPropertyType.Select) { Type type = modelType; this.ValueProperty = ReflectionHelper.GetProperty(ref type, aqa.ValuePropertyName); type = modelType; this.TextProperty = ReflectionHelper.GetProperty(ref type, aqa.TextPropertyName); this.EntityType = type; this.PropertyName = aqa.ValuePropertyName; this.DisplayName = aqa.DisplayName; } //有标签 非Select else { Type type = modelType; ReflectionHelper.GetProperty(ref type, aqa.AssociatedPropertyName); this.EntityType = type; this.PropertyName = aqa.AssociatedPropertyName; this.DisplayName = aqa.DisplayName; } }