Exemplo n.º 1
0
        // Dynamic field titles based on lambda expressions (experimental!)

        public static string DynamicFieldTitle(this HtmlHelper html, Expression <Func <object> > expression)
        {
            object entity;
            string fieldName;

            if (!ExpressionUtility.TryGetEntityAndFieldNameFromExpression(expression.Body, out entity, out fieldName))
            {
                throw new ArgumentException("DynamicField expression of unknown type: " + expression.Body.GetType().FullName + "\r\n" + expression.Body.ToString());
            }

            return(DynamicFieldTitle(html, entity, fieldName));
        }
        public virtual ActionResult List(string sort, int?show, int?page)
        {
            var query = EntitiesQuery;

            if (sort != null)
            {
                if (sort.StartsWith("-"))
                {
                    query = ExpressionUtility.ApplyOrderByDescendingClause(query, sort.Substring(1));
                }
                else
                {
                    query = ExpressionUtility.ApplyOrderByClause(query, sort);
                }
            }

            return(OnList(new PagedList <TEntity>(query, page ?? 1, show ?? 10), sort));
        }
Exemplo n.º 3
0
        public bool FillEntity(NameValueCollection parameters, object entity)
        {
            foreach (string propName in parameters)
            {
                MetaColumn column;
                if (!Table.TryGetColumn(propName, out column))
                {
                    continue;
                }

                string propValue = parameters[propName];
                ViewData.ModelState().Add(new ModelPropertyState(propName, propValue));

                if (propValue == String.Empty && column.ConvertEmptyStringToNull)
                {
                    propValue = null;
                }

                if (propValue == null && column.IsRequired)
                {
                    string error = column.RequiredErrorMessage;
                    if (String.IsNullOrEmpty(error))
                    {
                        error = String.Format("The field '{0}' is required", column.Name);
                    }

                    AddFieldError(column, error);
                    continue;
                }

                object value = null;

                var fkColumn = column as MetaForeignKeyColumn;
                if (fkColumn != null)      // For foreign keys, we need to get the parent entity in order to assign it
                {
                    IQueryable parentEntityQuery = ExpressionUtility.ApplyWhereClause(
                        fkColumn.ParentTable.GetQuery(Context),
                        fkColumn.ParentTable,
                        fkColumn.ParentTable.PrimaryKeyColumns[0],
                        propValue);

                    value = parentEntityQuery.FirstOrDefault();
                }
                else if (propValue != null)      // Convert it to the correct type for the field
                {
                    TypeConverter converter = TypeDescriptor.GetConverter(column.ColumnType);

                    try {
                        value = converter.ConvertFromString(propValue);
                    }
                    catch (Exception e) {
                        AddFieldError(column, e.Message);
                        continue;
                    }
                }

                if (propValue != null)
                {
                    foreach (var validationAttribute in column.Attributes.OfType <ValidationAttribute>())
                    {
                        if (!validationAttribute.IsValid(value))
                        {
                            AddFieldError(column, validationAttribute.FormatErrorMessage(column.Name));
                        }
                    }
                }

                try {
                    column.EntityTypeProperty.SetValue(entity, value, null);
                }
                catch (TargetInvocationException e) {
                    AddFieldError(column, e.InnerException.Message);
                    continue;
                }
            }

            return(!HasErrors);
        }