4
votes

I need to iterate the TValue type property in Blazor.

Component Tag as MYComponent.

<select>
@foreach (var item in this.Value(TValue))
        {
            <option selected value=@item></option>
        }
</select>

@code {
public class MYComponent {
           public TValue Value {get;set;}    
           private string[] MyValue = new string[] {"Value1", "Value2"};
}
}
1
What is TValue ? Where it's defined ? - agua from mars
Why do you use it as function ? - agua from mars
Tvalue is defined in top of the razor page @typeparam TValue; - M K
Update your question with the relevant code please. - agua from mars

1 Answers

1
votes

If I understand well your question, you can achieve it by using reflexion :

@foreach (var item in GetProperties(TValue))
{
   <option selected value=@item></option>
}
</select>
@code {
    public class MYComponent 
    {
        public TValue Value {get;set;}    
        private string[] MyValue = new string[] {"Value1", "Value2"};
        public IEnumerable<string> GetProperties()
        {
              return typeof(TValue).GetProperties().Select(p => p.Name);
        }
    }
}