Asp.net mvc 5 t4 woods

I use vs2013 and T4 templates to create views (Create, Edit, List) I need to get the Maxlength attribute.

Create.cs.t4 file I have the following code (snippet)

<#
foreach (PropertyMetadata property in ModelMetadata.Properties) {
  string inputlength = MaxLength(property);    
#>

    <#= property.PropertyName #> Maxlength is <#= inputlength  #>

<# } #>



string MaxLength(PropertyInfo property) {
    var stringLength = property.GetCustomAttributes(typeof(StringLengthAttribute), false);
    if (stringLength != null && stringLength.Length > 0) {
        return ((StringLengthAttribute)stringLength[0]).MaximumLength.ToString();
    } else {
        return "0";
    }
}

I know that I'm going to PropertyMetadata instead of PropertyInfo, but it was the closest I could get, but it does not return anything to me.

I don’t know if the conversion PropertyMetadata> PropertyInfo works, but I don’t know.

My goal is to see properties that have MaxLength = 1

Note: the structure of the templates is different from VS2013 VS2012, so I can not understand.

+4
source share

Source: https://habr.com/ru/post/1543258/


All Articles