Search code examples
c#asp.net-coreblazorcomponents

How to check if Parameter is binded in C# blazor?


I've written a component which has 30 or so parameters and would like to automatically setup this component when values are binded.

This works fine when a value is set, however some of the values are nullable and can be null. When given a value of 'null' the component cannot check if it's set, so I'm wondering:

Is it possible to check in the component if a variable is binded? If so, how?

if(DemoValue.isBinded) {}

Solution

  • The standard pattern for using SetParametersAsync is:

        public override Task SetParametersAsync(ParameterView parameters)
        {
            // You should always do this first
            parameters.SetParameterProperties(this);
    
            // do you stuff
    
            // pass an empty set of parameters to base as you've already set them
            return base.SetParametersAsync(ParameterView.Empty);
        }
    

    So your version can be updated to this.

        private List<string> bindedParameters = new();
        public override Task SetParametersAsync(ParameterView parameters)
        {
            parameters.SetParameterProperties(this);
    
            foreach (var parameter in parameters)
            {
                bindedParameters.Add(parameter.Name);
            }
    
            return base.SetParametersAsync(ParameterView.Empty);
        }