How to detect in code if a property is decorated with HiddenInput

I have an idea where I need to determine if a property is decorated with hidden input.

My property is defined as:

[HiddenInput(DisplayValue = false)]
public string UserName{ get; set; }

My attempt so far has been:

var column.Member = "UserName";

if (ViewData.ModelMetadata.HideSurroundingHtml == true && 
      ViewData.Values.Contains(column.Member))
{                          
  column.Visible = false;
}

I read that I could use "HideSurroundingHtml" to determine if a property should be displayed.

Any ideas how to detect this?

+5
source share
2 answers

My solution to this problem is as follows:

I created an html helper that gives me an array of names with properties that were decorated with the "HiddenInput" attribute.

   public static string[] GetListOfHiddenPropertiesFor<T>(this HtmlHelper htmlHelper)
        {
            Type t = typeof(T);
            var propertyInfos = t.GetProperties()
                                .Where(x => Attribute.IsDefined(x, typeof(HiddenInputAttribute)))
                                .Select(x => x.Name).ToArray();
            return propertyInfos;
        }

it's all I need

0
source

You can use reflection to find out if a particular property has an attribute.

.

, , . , .

+2

All Articles