5

I have a view where I need to detect 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 have read that I might be able to use "HideSurroundingHtml" to determine if the property should not be displayed.

Any ideas how to detect this?

Iridio
  • 9,213
  • 4
  • 49
  • 71
cpoDesign
  • 8,953
  • 13
  • 62
  • 106

2 Answers2

2

You can use reflection to see if a specific property has an attribute.

Look at this question.

In the various answers a user also posted a snippet to create an extension method to check if a property has a specific attribute or not. Hope it helps

Community
  • 1
  • 1
Iridio
  • 9,213
  • 4
  • 49
  • 71
0

My solution to this problem is as follows:

I have created html helper that gives me array of names with properties that has been 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;
        }

this is all i needed

cpoDesign
  • 8,953
  • 13
  • 62
  • 106