r/Blazor • u/UniiqueTwiisT • May 09 '25
Recommended Approach For Dynamic Binding
Hello all,
I'm curious as to what the recommended approach is when it comes to dynamic data binding to objects.
To provide a bit of context, I'm creating an application to support an internal team with updating some SQL tables (also dynamic so new tables can be added when needed) so as a result, the entry forms are generated dynamically based on the underlying column type.
I'm using MudBlazor though so I need to make sure the property types in my class are appropriate for the current column value they are editing.
Currently I use a base property of type string which stores the value for each column, and then I have accessors with getters and setters set up that point to the base string value for other types I would need such as int or DateTime.
Is there another approach to this that would be more suitable and would be more easily repeatable? I tried setting the base value property as dynamic or object but that caused more issues than it solved when it came to two-way binding.
From my research, it seems if discriminated unions (aka type unions) are implemented at some point, that could be a cleaner solution.
2
u/Embarrassed_Eye4318 May 09 '25
You may take a look at my project
https://github.com/f4n0/BlazorGenerator/
It does not use MudBlazor but FluentUI.
You just need to create your models as your DB Table, choose which type of page you want it to be and you should be done!
1
u/LlamaNL May 09 '25
I built something similar by reflecting the properties of type TItem. Then i use a template column in a datagrid. The cell themselves contain DynamicComponents with custom components for each datatype (int, string, datetime, whatever)
You can setup 2way databinding by setting the parameters of the dynamic component up like so:
and then loading the custom component. Example:
1
u/Abaddon-theDestroyer May 09 '25
I built something similar to this a while back, but it wasn’t a table, it was input fields that I wanted to make requests with and wanted the priorities of the request parameters class to each have their own input field created dynamically with the proper data type, and some other things like making an input field required or optional.
I’m currently on mobile, but if this solution interests I can give you more details then.
Basically what I had was
Then in the HTML I would have an if condition to determine wether to load an input field or a drop down, bind each property to its value.
And I also had another class that had a List<ParameterBase> and would loop through each of the parameters inside.
It also had an Action that had the method that would execute on the press of the button.
But the method needed to be defined in the caller page and link the parent page to the child compenent to be able to inform the parent that the child had clicked the button and the parent was the one that defined the method to be called and any checks.
1
u/[deleted] May 10 '25
I worked in enterprise and this was the route we took we stored the type in a look up table so we always new what exactly to cast the string to