/// <summary>
        /// Determine if the property is a read only hyperlink.
        /// </summary>
        /// <param name="modelMetadata">The <see cref="ModelMetadata"/> instance.</param>
        /// <returns><c>true</c> if the property is a read only hyperlink; otherwise, <c>false</c>.</returns>
        public static bool IsReadOnlyHyperlink(this ModelMetadata modelMetadata)
        {
#if DEBUG
            var step = MiniProfiler.Current.Step("ModelMetadata.IsReadOnlyHyperlink");

            try
            {
#endif
            // Check property is readonly and has a link or external link
            if (modelMetadata.IsReadOnly && !modelMetadata.IsViewModel() && (modelMetadata.HasLink() || modelMetadata.HasExternalLink()))
            {
                EditableAttribute editable = modelMetadata.GetAttribute <EditableAttribute>();
                ReadOnlyAttribute readOnly = modelMetadata.GetAttribute <ReadOnlyAttribute>();

                // Property must ALWAYS be read only to render as a hyperlink
                if ((editable != null && !editable.AllowEdit) || (readOnly != null && readOnly.IsReadOnly))
                {
                    var notDefault = ComparisonType.NotEqualTo.Compare(modelMetadata.Model, modelMetadata.ModelType.GetDefaultValue());
                    var selectList = modelMetadata.Model as IEnumerable <SelectListItem>;

                    if (selectList != null)
                    {
                        // Handle "not default value" for IEnumerable<SelectListItem> properties based on whether there are selections
                        notDefault = selectList.Any(p => p.Selected);
                    }

                    // Only a hyperlink if the model has a value
                    if (modelMetadata.ModelType == typeof(string) ? !string.IsNullOrEmpty(modelMetadata.Model as string) : notDefault)
                    {
                        return(true);
                    }
                }
            }

            return(false);

#if DEBUG
        }

        finally
        {
            if (step != null)
            {
                step.Dispose();
            }
        }
#endif
        }
Exemplo n.º 2
0
        /// <summary>
        /// Get selection type
        /// </summary>
        /// <param name="modelMetadata"></param>
        /// <param name="parentModelPropertyMetadata"></param>
        /// <returns></returns>
        public static SelectionType GetSelectionType(ModelMetadata modelMetadata, ModelMetadata parentModelPropertyMetadata)
        {
 #if DEBUG
            var step = MiniProfiler.Current.Step("GridHelper.GetSelectionType");

            try
            {
#endif
            // Check for SelectionType on property in parent model
            var selectionTypeAttribute = parentModelPropertyMetadata.GetAttribute <SelectionTypeAttribute>();

            if (selectionTypeAttribute != null)
            {
                return(selectionTypeAttribute.SelectionType);
            }

            // If not found, check for SelectionType on model
            selectionTypeAttribute = modelMetadata.GetAttribute <SelectionTypeAttribute>();

            return((selectionTypeAttribute != null) ? selectionTypeAttribute.SelectionType : SelectionType.Default);

#if DEBUG
        }

        finally
        {
            if (step != null)
            {
                step.Dispose();
            }
        }
#endif
        }
        public void GetAttributeTest()
        {
            var provider  = new DataAnnotationsModelMetadataProvider();
            var modelType = new TypeDelegator(typeof(TestModel));
            var t         = new ModelMetadata(provider, null, null, modelType, string.Empty);

            t.AdditionalValues["Attributes"] = new List <DisplayAttribute>()
            {
                new DisplayAttribute()
            };
            var a = t.GetAttribute <DisplayAttribute>();

            Assert.IsNotNull(a);
        }
        public void GetAttribute_PredicateTest()
        {
            var provider  = new DataAnnotationsModelMetadataProvider();
            var modelType = new TypeDelegator(typeof(TestModel));
            var t         = new ModelMetadata(provider, null, null, modelType, string.Empty);

            t.AdditionalValues["Attributes"] = new List <DisplayAttribute>()
            {
                new DisplayAttribute()
                {
                    Name = "TestField", Order = 1
                }
            };
            var a = t.GetAttribute <DisplayAttribute>(p => p.Name.Equals("TestField"));

            Assert.IsNotNull(a);
            Assert.AreEqual("TestField", a.Name);
            Assert.AreEqual(1, a.Order);
        }
        /// <summary>
        /// Gets the default value based on Model Metadata and <see cref="DefaultValueAttribute"/>.
        /// </summary>
        /// <typeparam name="T">Type of value.</typeparam>
        /// <param name="modelMetadata"><see cref="ModelMetadata"/> instance.</param>
        /// <param name="value">The default value.</param>
        /// <returns><c>true</c> if there is a default value; otherwise, <c>false</c>.</returns>
        public static bool TryGetDefaultValue <T>(this ModelMetadata modelMetadata, out T value)
        {
            value = default(T);
            var defaultValue = modelMetadata.GetAttribute <DefaultValueAttribute>();

            if (defaultValue == null)
            {
                return(false);
            }

            try
            {
                value = (T)defaultValue.Value;
            }
            catch (InvalidCastException ex)
            {
                return(false);
            }

            return(true);
        }
        /// <summary>
        /// Whether the client-side unsaved changes prompt should always be skipped.
        /// </summary>
        /// <param name="modelMetadata">The <see cref="ModelMetadata"/> instance.</param>
        /// <returns><c>true</c> if the client-side unsaved changes prompt should always be skipped; otherwise, <c>false</c>.</returns>
        public static bool SkipClientSideUnsavedChanges(this ModelMetadata modelMetadata)
        {
            var attribute = modelMetadata.GetAttribute <SkipClientSideAttribute>();

            return(attribute != null && attribute.UnsavedChanges);
        }
        /// <summary>
        /// Whether the client-side validation should always be skipped.
        /// </summary>
        /// <param name="modelMetadata">The <see cref="ModelMetadata"/> instance.</param>
        /// <returns><c>true</c> if the client-side validation should always be skipped; otherwise, <c>false</c>.</returns>
        public static bool SkipClientSideValidation(this ModelMetadata modelMetadata)
        {
            var attribute = modelMetadata.GetAttribute <SkipClientSideAttribute>();

            return(attribute != null && attribute.Validation);
        }
        /// <summary>
        /// Determine if the property is a ViewModel or enumerable ViewModel.
        /// </summary>
        /// <param name="modelMetadata">The <see cref="ModelMetadata"/> instance.</param>
        /// <returns><c>true</c> if the property is a ViewModel or enumerable ViewModel; otherwise, <c>false</c>.</returns>
        public static bool IsViewModel(this ModelMetadata modelMetadata)
        {
#if DEBUG
            var step = MiniProfiler.Current.Step("ModelMetadata.IsViewModel");

            try
            {
#endif
            var isViewModel = (modelMetadata.ModelType != null && modelMetadata.ModelType.FullName != null && modelMetadata.ModelType.FullName.EndsWith("ViewModel", StringComparison.Ordinal)) || modelMetadata.GetAttribute <ViewModelAttribute>() != null;

            if (!isViewModel)
            {
                var underlyingType = modelMetadata.ModelType.GetUnderlyingType();

                if (underlyingType != null)
                {
                    isViewModel = (underlyingType.FullName != null && underlyingType.FullName.EndsWith("ViewModel", StringComparison.Ordinal)) || TypeDescriptor.GetAttributes(underlyingType).OfType <ViewModelAttribute>().Any();
                }
            }

            return(isViewModel);

#if DEBUG
        }

        finally
        {
            if (step != null)
            {
                step.Dispose();
            }
        }
#endif
        }