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

Create a form that automatically generate fields according to a generic Type

$
0
0

I'm using the Form Telerik component and I would like to use the template customization for every form item. Now, I need to create a component called InsertEntity which take a type TEntity as input and then recreate the form according to the fields of the properties of TEntity. The problem here is that i'm not able to use the @bind-value with the generic type. I follow also this tutorial: Blazor : is it possible to bind a generic item to a property by its name using reflection? but it doesn't work with TelerikTextBox.

I would like a demo from you with a form that dynamically show the property of a type using the customization template of the form...

Something like the code attached....

@inject IStringLocalizer<InsertEntity<TEntity>> Localizer@typeparam TEntity where TEntity : class, IValidable<TEntity>, IHub<TEntity>, new()<TelerikForm EditContext="@FormEditContext" OnValidSubmit="@OnValidSubmitHandler" Class="m-auto mb-5" Model="@EditableEntityDTO" Columns="@(ScreenSize == MediaQuery.Medium ? 1 : 2)" ColumnSpacing="25px" Width="80%"><FormValidation><TelerikValidationSummary><Template><div class="custom-validation-message" style="color:red">                    @if (!string.IsNullOrEmpty(CustomValidationMessage))                    {<div><TelerikSvgIcon Icon="@SvgIcon.XOutline" /><span>@Localizer[CustomValidationMessage]</span></div>                    }</div></Template></TelerikValidationSummary></FormValidation><FormItems>        @{            foreach (var property in Properties)            {                if (property.PropertyType == typeof(System.String))                {<FormItem Field="@nameof(property.Name)"><Template><label for="@nameof(property)" class="k-label k-form-label">@(ModelHelper.GetAttributeValue<TEntity, DisplayAttribute>(property.Name))</label><div class="k-form-field-wrap"><TelerikTextBox Placeholder=@(ModelHelper.GetAttributeValue<TEntity, PlaceholderAttribute>(nameof(property)))                                            @bind-Value="????????????????" /><TelerikValidationMessage For="@(() => property)" /></div></Template></FormItem>                }                else if (property.PropertyType == typeof(DateTime) || property.PropertyType == typeof(DateTime?))                {                }                else if (property.PropertyType.IsEnum)                {                }            }        }</FormItems></TelerikForm>

Viewing all articles
Browse latest Browse all 4839

Trending Articles



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