Blazor InputText with maxlength based on dbcontext Table Column size

426 Views Asked by At

I've been looking at how to automate a Blazor InputText maxlength attribute to a dbcontext table column size (without using DataAnnotation which repeats information) and have come up with a solution - but is this the best way to achieve it?

1

There are 1 best solutions below

0
On

Thank you for your helpful comments - after 50 years of programming I don't keep up with everything. I have amended the class to be very simple but to achieve what I want.

As an SQL-first developer, I already have much in the way of information in the database and I really don't use DataAnnotations to replicate this anymore.

I was hoping the dbcontext function would be more useful to someone more than the input component - which I could have thought more about.

<InputTextDb @bind-Value="SomeValue" MaxLength="@db.GetStringColumnLength("TableName.ColumnName")" />

    public class InputTextDb : InputText
    {
        [Parameter]
        public int? MaxLength { get; set; }

        protected override void BuildRenderTree(RenderTreeBuilder builder)
        {
            if (MaxLength.HasValue)
            {
                Dictionary<string, object> dic = new()
                {
                    { "maxlength", MaxLength.Value }
                };

                this.AdditionalAttributes = dic;
            }

            base.BuildRenderTree(builder);
        }
    }