Exemplo n.º 1
0
        private TagBuilder ApplyTrColumnHtml(Action <KeyValuePair <string, PropertyInfo>, DisplayAttribute, List <TagBuilder>, object> action, object value)
        {
            TagBuilder        trBuilder  = new TagBuilder("tr");
            List <TagBuilder> listColumn = new List <TagBuilder>();

            typeof(TModel).GetSortedProperties().ToDictionary(x => x.Name, x => x).ForEach(pair =>
            {
                DisplayAttribute display    = pair.Value.GetCustomAttribute <DisplayAttribute>();
                HiddenInputAttribute hidden = pair.Value.GetCustomAttribute <HiddenInputAttribute>();
                RadioStateAttribute state   = pair.Value.GetCustomAttribute <RadioStateAttribute>();
                if (state == null)
                {
                    if (hidden == null || hidden.DisplayValue == true)
                    {
                        if (display == null || display.GetAutoGenerateField() == null || display.AutoGenerateField == true)
                        {
                            action(pair, display, listColumn, value);
                        }
                    }
                }
            });

            listColumn.ForEach(d => trBuilder.InnerHtml += d);
            return(trBuilder);
        }
        public void TemplateHint_AttributesHaveExpectedPrecedence()
        {
            // Arrange
            var expected = "this is a hint";
            var hidden   = new HiddenInputAttribute();
            var uiHint   = new UIHintAttribute(expected);
            var provider = CreateProvider(new object[] { hidden, uiHint, });

            var metadata = provider.GetMetadataForType(typeof(string));

            // Act
            var result = metadata.TemplateHint;

            // Assert
            Assert.Equal(expected, result);
        }
Exemplo n.º 3
0
        /// <exclude/>
        public TableBuilderT(string id = null, GridTreeDataModel model = null, string url = null, TablePaginationOption sidePagination = TablePaginationOption.none, object htmlAttributes = null)
            : base(id, model, url, sidePagination, htmlAttributes)
        {
            typeof(TModel).GetSortedProperties().ToDictionary(x => x.Name, x => x).ForEach(pair =>
            {
                DisplayAttribute display    = pair.Value.GetCustomAttribute <DisplayAttribute>();
                HiddenInputAttribute hidden = pair.Value.GetCustomAttribute <HiddenInputAttribute>();

                if (hidden == null || hidden.DisplayValue == true)
                {
                    if (display == null || display.GetAutoGenerateField() == null || display.AutoGenerateField == true)
                    {
                        if (display != null && !string.IsNullOrEmpty(display.GetName()))
                        {
                            Column(display.GetName(), pair.Key);
                        }
                        else
                        {
                            Column(pair.Key.SplitCamelCase(), pair.Key);
                        }
                    }
                }
            });
        }
        public void TemplateHint_AttributesHaveExpectedPrecedence()
        {
            // Arrange
            var expected = "this is a hint";
            var hidden = new HiddenInputAttribute();
            var uiHint = new UIHintAttribute(expected);
            var provider = CreateProvider(new object[] { hidden, uiHint, });

            var metadata = provider.GetMetadataForType(typeof(string));

            // Act
            var result = metadata.TemplateHint;

            // Assert
            Assert.Equal(expected, result);
        }
        public async Task <ActionResult> CreateRespondent([Bind(Include = "Id,RespondentNumber,FirstName,LastName,IsModified,IsDeleted")] Respondent respondent, HiddenInputAttribute hidden)
        {
            Group group;

            if (ModelState.IsValid)
            {
                int groupId = int.Parse(Request.Params["GroupId"]);

                group = await db.Groups.Where(g => g.Id == groupId).FirstOrDefaultAsync();

                respondent.IsModified = true;
                group.Respondents.Add(respondent);

                db.Respondents.Add(respondent);
                await db.SaveChangesAsync();

                return(RedirectToAction("ViewRespondents", new { id = groupId }));
            }
            return(View());
        }
        //public override IEnumerable<ModelMetadata> GetMetadataForProperties(object container, Type containerType)
        //{
        //  return base.GetMetadataForProperties(container, containerType);
        //}

        //public override ModelMetadata GetMetadataForType(Func<object> modelAccessor, Type modelType)
        //{
        //  return base.GetMetadataForType(modelAccessor, modelType);
        //}

        //public override ModelMetadata GetMetadataForProperty(Func<object> modelAccessor, Type containerType, string propertyName)
        //{
        //  return base.GetMetadataForProperty(modelAccessor, containerType, propertyName);
        //}

        //protected override ModelMetadata GetMetadataForProperty(Func<object> modelAccessor, Type containerType, System.ComponentModel.PropertyDescriptor propertyDescriptor)
        //{
        //  return base.GetMetadataForProperty(modelAccessor, containerType, propertyDescriptor);
        //}
        protected override ModelMetadata CreateMetadata(IEnumerable <Attribute> attributes, Type containerType, Func <object> modelAccessor, Type modelType, string propertyName)
        {
            //Reflected from base class
            List <Attribute>       list = new List <Attribute>(attributes);
            DisplayColumnAttribute displayColumnAttribute = Enumerable.FirstOrDefault <DisplayColumnAttribute>(Enumerable.OfType <DisplayColumnAttribute>((IEnumerable)list));

            DataAnnotationsModelMetadata annotationsModelMetadata = null;

            //brl additions
            //if this is a BOV backed object
            if (containerType != null && Attribute.IsDefined(containerType, typeof(DryLogicObjectAttribute)) && propertyName != "OI")
            {
                Object         container = null;
                ObjectInstance oi        = null;
                //By having this code here instead of in GetMetadataForProperty, some of the normal features like ui hint will work
                if (modelAccessor != null)
                {
                    var rootModelType = modelAccessor.Target.GetType();
                    var field         = rootModelType.GetField("container");
                    if (field != null)
                    {
                        container = field.GetValue(modelAccessor.Target);
                        //if we don't have a reference to the container yet...
                        if (container.GetType() != containerType)
                        {
                            //...then try to break down the expression to get it
                            //get the expression as text, ie "model.EmployeeViewModel.MyEmployee" and split it
                            var expressionParts = ((LambdaExpression)rootModelType.GetField("expression").GetValue(modelAccessor.Target)).Body.ToString().Split('.');
                            //var expressionParts = new string[] { };

                            //loop thru the parts in the middle
                            for (int i = 1; i < expressionParts.Length - 1; i++)
                            {
                                container = container.GetType().GetProperty(expressionParts[i]).GetValue(container);
                            }
                        }
                        //could use an attribute instead to identify the object instance
                        oi = ObjectInstance.GetObjectInstance(container);

                        if (oi != null)//not really sure how this woudl fail at this point
                        {
                            annotationsModelMetadata           = new PropertyValueMetadata(this, containerType, modelAccessor, modelType, propertyName, displayColumnAttribute);
                            annotationsModelMetadata.Container = container;
                            //internally, setting model wipes out modelAccessor (caching of sorts)
                            annotationsModelMetadata.Model        = oi.PropertyValues[propertyName];
                            annotationsModelMetadata.TemplateHint = "PropertyValue";
                        }
                    }
                }
            }
            if (annotationsModelMetadata == null)
            {
                annotationsModelMetadata = new DataAnnotationsModelMetadata(this, containerType, modelAccessor, modelType, propertyName, displayColumnAttribute);
            }


            HiddenInputAttribute hiddenInputAttribute = Enumerable.FirstOrDefault <HiddenInputAttribute>(Enumerable.OfType <HiddenInputAttribute>((IEnumerable)list));

            if (hiddenInputAttribute != null)
            {
                annotationsModelMetadata.TemplateHint        = "HiddenInput";
                annotationsModelMetadata.HideSurroundingHtml = !hiddenInputAttribute.DisplayValue;
            }
            IEnumerable <UIHintAttribute> source = Enumerable.OfType <UIHintAttribute>((IEnumerable)list);
            UIHintAttribute uiHintAttribute      = Enumerable.FirstOrDefault <UIHintAttribute>(source, (Func <UIHintAttribute, bool>)(a => string.Equals(a.PresentationLayer, "MVC", StringComparison.OrdinalIgnoreCase))) ?? Enumerable.FirstOrDefault <UIHintAttribute>(source, (Func <UIHintAttribute, bool>)(a => string.IsNullOrEmpty(a.PresentationLayer)));

            if (uiHintAttribute != null)
            {
                annotationsModelMetadata.TemplateHint = uiHintAttribute.UIHint;
            }
            DataTypeAttribute attribute = Enumerable.FirstOrDefault <DataTypeAttribute>(Enumerable.OfType <DataTypeAttribute>((IEnumerable)list));

            if (attribute != null)
            {
                annotationsModelMetadata.DataTypeName = DataTypeUtil.ToDataTypeName(attribute, (Func <DataTypeAttribute, bool>)null);
            }
            EditableAttribute editableAttribute = Enumerable.FirstOrDefault <EditableAttribute>(Enumerable.OfType <EditableAttribute>((IEnumerable)attributes));

            if (editableAttribute != null)
            {
                annotationsModelMetadata.IsReadOnly = !editableAttribute.AllowEdit;
            }
            else
            {
                ReadOnlyAttribute readOnlyAttribute = Enumerable.FirstOrDefault <ReadOnlyAttribute>(Enumerable.OfType <ReadOnlyAttribute>((IEnumerable)list));
                if (readOnlyAttribute != null)
                {
                    annotationsModelMetadata.IsReadOnly = readOnlyAttribute.IsReadOnly;
                }
            }
            DisplayFormatAttribute displayFormatAttribute = Enumerable.FirstOrDefault <DisplayFormatAttribute>(Enumerable.OfType <DisplayFormatAttribute>((IEnumerable)list));

            if (displayFormatAttribute == null && attribute != null)
            {
                displayFormatAttribute = attribute.DisplayFormat;
            }
            if (displayFormatAttribute != null)
            {
                annotationsModelMetadata.NullDisplayText          = displayFormatAttribute.NullDisplayText;
                annotationsModelMetadata.DisplayFormatString      = displayFormatAttribute.DataFormatString;
                annotationsModelMetadata.ConvertEmptyStringToNull = displayFormatAttribute.ConvertEmptyStringToNull;
                if (displayFormatAttribute.ApplyFormatInEditMode)
                {
                    annotationsModelMetadata.EditFormatString = displayFormatAttribute.DataFormatString;
                }
                if (!displayFormatAttribute.HtmlEncode && string.IsNullOrWhiteSpace(annotationsModelMetadata.DataTypeName))
                {
                    annotationsModelMetadata.DataTypeName = DataTypeUtil.HtmlTypeName;
                }
            }
            ScaffoldColumnAttribute scaffoldColumnAttribute = Enumerable.FirstOrDefault <ScaffoldColumnAttribute>(Enumerable.OfType <ScaffoldColumnAttribute>((IEnumerable)list));

            if (scaffoldColumnAttribute != null)
            {
                annotationsModelMetadata.ShowForDisplay = annotationsModelMetadata.ShowForEdit = scaffoldColumnAttribute.Scaffold;
            }
            DisplayAttribute displayAttribute = Enumerable.FirstOrDefault <DisplayAttribute>(Enumerable.OfType <DisplayAttribute>((IEnumerable)attributes));
            string           str = (string)null;

            if (displayAttribute != null)
            {
                annotationsModelMetadata.Description      = displayAttribute.GetDescription();
                annotationsModelMetadata.ShortDisplayName = displayAttribute.GetShortName();
                annotationsModelMetadata.Watermark        = displayAttribute.GetPrompt();
                annotationsModelMetadata.Order            = displayAttribute.GetOrder() ?? 10000;
                str = displayAttribute.GetName();
            }
            if (str != null)
            {
                annotationsModelMetadata.DisplayName = str;
            }
            else
            {
                DisplayNameAttribute displayNameAttribute = Enumerable.FirstOrDefault <DisplayNameAttribute>(Enumerable.OfType <DisplayNameAttribute>((IEnumerable)list));
                if (displayNameAttribute != null)
                {
                    annotationsModelMetadata.DisplayName = displayNameAttribute.DisplayName;
                }
            }
            if (Enumerable.FirstOrDefault <RequiredAttribute>(Enumerable.OfType <RequiredAttribute>((IEnumerable)list)) != null)
            {
                annotationsModelMetadata.IsRequired = true;
            }
            return((ModelMetadata)annotationsModelMetadata);
        }
Exemplo n.º 7
0
        protected override ModelMetadata CreateMetadata(IEnumerable <Attribute> attributes, Type containerType, Func <object> modelAccessor, Type modelType, string propertyName)
        {
            List <Attribute>             attributeList          = new List <Attribute>(attributes);
            DisplayColumnAttribute       displayColumnAttribute = attributeList.OfType <DisplayColumnAttribute>().FirstOrDefault();
            DataAnnotationsModelMetadata result = new DataAnnotationsModelMetadata(this, containerType, modelAccessor, modelType, propertyName, displayColumnAttribute);

#if UNDEF
            // Do [HiddenInput] before [UIHint], so you can override the template hint
            HiddenInputAttribute hiddenInputAttribute = attributeList.OfType <HiddenInputAttribute>().FirstOrDefault();
            if (hiddenInputAttribute != null)
            {
                result.TemplateHint        = "HiddenInput";
                result.HideSurroundingHtml = !hiddenInputAttribute.DisplayValue;
            }
#endif

            // We prefer [UIHint("...", PresentationLayer = "MVC")] but will fall back to [UIHint("...")]
            IEnumerable <UIHintAttribute> uiHintAttributes = attributeList.OfType <UIHintAttribute>();
            UIHintAttribute uiHintAttribute = uiHintAttributes.FirstOrDefault(a => String.Equals(a.PresentationLayer, "MVC", StringComparison.OrdinalIgnoreCase))
                                              ?? uiHintAttributes.FirstOrDefault(a => String.IsNullOrEmpty(a.PresentationLayer));
            if (uiHintAttribute != null)
            {
                result.TemplateHint = uiHintAttribute.UIHint;
            }

            DataTypeAttribute dataTypeAttribute = attributeList.OfType <DataTypeAttribute>().FirstOrDefault();
            if (dataTypeAttribute != null)
            {
                result.DataTypeName = dataTypeAttribute.ToDataTypeName();
            }

            EditableAttribute editable = attributes.OfType <EditableAttribute>().FirstOrDefault();
            if (editable != null)
            {
                result.IsReadOnly = !editable.AllowEdit;
            }
            else
            {
                ReadOnlyAttribute readOnlyAttribute = attributeList.OfType <ReadOnlyAttribute>().FirstOrDefault();
                if (readOnlyAttribute != null)
                {
                    result.IsReadOnly = readOnlyAttribute.IsReadOnly;
                }
            }

            DisplayFormatAttribute displayFormatAttribute = attributeList.OfType <DisplayFormatAttribute>().FirstOrDefault();
            if (displayFormatAttribute == null && dataTypeAttribute != null)
            {
                displayFormatAttribute = dataTypeAttribute.DisplayFormat;
            }
            if (displayFormatAttribute != null)
            {
                result.NullDisplayText          = displayFormatAttribute.NullDisplayText;
                result.DisplayFormatString      = displayFormatAttribute.DataFormatString;
                result.ConvertEmptyStringToNull = displayFormatAttribute.ConvertEmptyStringToNull;

                if (displayFormatAttribute.ApplyFormatInEditMode)
                {
                    result.EditFormatString = displayFormatAttribute.DataFormatString;
                }

                if (!displayFormatAttribute.HtmlEncode && String.IsNullOrWhiteSpace(result.DataTypeName))
                {
                    result.DataTypeName = DataTypeUtil.HtmlTypeName;
                }
            }

            ScaffoldColumnAttribute scaffoldColumnAttribute = attributeList.OfType <ScaffoldColumnAttribute>().FirstOrDefault();
            if (scaffoldColumnAttribute != null)
            {
                result.ShowForDisplay = result.ShowForEdit = scaffoldColumnAttribute.Scaffold;
            }

            DisplayAttribute display = attributes.OfType <DisplayAttribute>().FirstOrDefault();
            string           name    = null;
            if (display != null)
            {
                var displayAdapter = new DisplayAttributeAdapter(display);
                result.Description      = displayAdapter.GetDescription();
                result.ShortDisplayName = displayAdapter.GetShortName();
                result.Watermark        = displayAdapter.GetPrompt();
                result.Order            = displayAdapter.GetOrder() ?? ModelMetadata.DefaultOrder;

                name = displayAdapter.GetName();
            }

            if (name != null)
            {
                result.DisplayName = name;
            }
            else
            {
                DisplayNameAttribute displayNameAttribute = attributeList.OfType <DisplayNameAttribute>().FirstOrDefault();
                if (displayNameAttribute != null)
                {
                    result.DisplayName = displayNameAttribute.DisplayName;
                }
            }

            RequiredAttribute requiredAttribute = attributeList.OfType <RequiredAttribute>().FirstOrDefault();
            if (requiredAttribute != null)
            {
                result.IsRequired = true;
            }

            return(result);
        }