Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

In blazor, using @bind-checked on a loop of property info via reflection of a custom class

$
0
0

I have a class, MyModel, with over 100 properties (I know) that are all of bool? data type. I'm trying to dynamically generate two radio buttons (Yes / No) for each property instead of manually creating the controls for each. I'm able to achieve some of my desired output, but not bind the data with validation. The edit-form uses either a model or an edit context, I've tried both. (using syncfusion DataForm component)

First I tried just looping of the reflected property info.

@foreach (var prop in properties){<label>@prop.name</label><radio button value=true @bind-checked=@prop.SetValue(MyModel)><radio button value=false @bind-checked=@prop.SetValue(MyModel)>}

Next I tried using a dictionary to store the field name and bool value, but dictionaries are read-only in this context and cannot be two-way binded.

var dict = new Dictionary<PropertyInfo, bool?>;(reflection here to set dictionary, setting bool? to null) @foreach (var kvp in dict){<label>@kvp.Key.Name</label><radio button value=true @bind-checked=@kvp.value><radio button value=false @bind-checked=@kvp.value>}

Then I changed the dictionary to include a 'wrapper' object so the value can be changed. This works except then I cant use the edit context/ model for validation.

var dict = new Dictionary<PropertyInfo, BoolWrapper?>;@foreach (var kvp in dict){<label>@kvp.Key.Name</label><radio button value=true @bind-checked=@kvp.value.YesNo><radio button value=false @bind-checked=@kvp.value.YesNo>}public class BoolWrapper{public bool? YesNo {get; set;}}

I'm open to any ideas on how to make this possible. I considered making the model class indexable, but I'm still pretty new to C# and it adds a lot of complexity that I can't quite understand.

Any ideas on how to make this possible is greatly appreciated.


Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>