示例#1
0
        /// <summary>
        /// BuildQueryCondition
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="TValue"></typeparam>
        /// <param name="pagingModel"></param>
        /// <returns></returns>
        public static Expression <Func <T, bool> > BuildQueryCondition <T>(PagingModel pagingModel)
        {
            var expression = PredicateBuilder.True <T>();

            if (!string.IsNullOrWhiteSpace(pagingModel.Query) && !string.IsNullOrWhiteSpace(pagingModel.QueryFields))
            {
                expression = LambdaHelper.BuildLambdasOr <T>(pagingModel.QueryFields, QueryMethods.Contains, pagingModel.Query);
            }

            if (pagingModel.AdvancedQuery != null && pagingModel.AdvancedQuery.Count() > 0)
            {
                Expression <Func <T, bool> > tmpExpr = null;
                var expr = PredicateBuilder.True <T>();
                foreach (var item in pagingModel.AdvancedQuery)
                {
                    tmpExpr = LambdaHelper.BuildLambda <T, bool>(item.Field, item.Operator, item.Value);
                    if (tmpExpr != null)
                    {
                        expr = expr.And(tmpExpr);
                    }
                }
                expression = expression.And(expr);
            }
            return(expression);
        }
        /// <summary>
        /// SiDropDownListFor
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <typeparam name="TProperty"></typeparam>
        /// <param name="htmlHelper"></param>
        /// <param name="expression"></param>
        /// <returns></returns>
        public static MvcHtmlString SiDropDownListFor <TModel, TProperty>(this HtmlHelper <TModel> htmlHelper, Expression <Func <TModel, TProperty> > expression)
        {
            Type         type         = typeof(TModel);
            string       inferredName = ExpressionHelper.GetExpressionText(expression);
            PropertyInfo pi           = type.GetProperty(inferredName);

            IEnumerable <SelectListItem> selectList = new List <SelectListItem>();
            object v = null;

            if (typeof(TProperty).GetProperty("Id") != null)
            {
                IEnumerable <TProperty> list = new List <TProperty>();
                if (pi.IsDefined(typeof(RelationAttribute), false))
                {
                    Attribute attr = Attribute.GetCustomAttribute(pi, typeof(RelationAttribute), false);
                    if (attr != null)
                    {
                        string   condition = ((RelationAttribute)attr).Condition;
                        string[] c         = condition.Split(new char[] { '=' });
                        var      expr      = LambdaHelper.BuildLambda <TProperty, bool>(c[0], QueryMethods.Equals, c[1]);

                        object     o          = Activator.CreateInstance(typeof(TProperty));
                        MethodInfo methodInfo = typeof(TProperty).GetMethod("GetList", new Type[] { typeof(Expression <Func <TProperty, bool> >) });
                        list = methodInfo.Invoke(o, new object[] { expr }) as IEnumerable <TProperty>;
                    }
                }
                v = htmlHelper.ViewContext.ViewData.Eval(string.Format("{0}Id", inferredName)) ?? "";

                var code = LambdaHelper.BuildPropertySpecifier <TProperty, int>(string.Format("{0}.Id", inferredName));
                var name = LambdaHelper.BuildPropertySpecifier <TProperty, string>(string.Format("{0}.Name", inferredName));

                Mapper.CreateMap <TProperty, SelectListItem>()
                .ForMember(dest => dest.Text, opt => opt.MapFrom(name))
                .ForMember(dest => dest.Value, opt => opt.MapFrom(code));

                selectList = Mapper.Map <IEnumerable <TProperty>, IEnumerable <SelectListItem> >(list);
            }
            else
            {
                v = htmlHelper.ViewContext.ViewData.Eval(inferredName) ?? "";
                if (pi.IsDefined(typeof(ValuesAttribute), false))
                {
                    Attribute attr = Attribute.GetCustomAttribute(pi, typeof(ValuesAttribute), false);
                    if (attr != null)
                    {
                        string value = ((ValuesAttribute)attr).Value;
                        selectList = JsonConvert.DeserializeObject <IEnumerable <SelectListItem> >(value);
                    }
                }
            }
            var selected = selectList.Where(p => p.Value == v.ToString()).SingleOrDefault();

            if (selected != null)
            {
                selected.Selected = true;
            }

            return(SiDropDownListFor(htmlHelper, expression, selectList));
        }