parametersblazorparameter-passingboolean-logicboolean-expression

Blazor [Parameter] bool is always returning false


I inserted a parameter bool in select.cs and set its value to true in razor page but it always returns false.

In select.cs:

public class NameSelect<TItemValue> : CachedSelect<TItemValue, AgLookupDto>
{
    [Parameter]
    public bool Proceed { get; set; }

    public NameSelect()
    {
        if (typeof(TItemValue) == typeof(string))
            ValueName = nameof(AgLookupDto.Code);
        else
            ValueName = nameof(AgLookupDto.Id);

        LabelName = nameof(AgLookupDto.Code);
        

        Console.WriteLine(Proceed);
}}

In Page.razor

<AntDesign.Col Md="5" Xs="24">
    <FormItem Label="Ag">
        <NameSelect @bind-Value="@context.AgId" Item="@context.Ag" Proceed="true">
        </NameSelect>
    </FormItem>
</AntDesign.Col>

Solution

  • This part is a constructor:

    public NameSelect()
    {
        ...
    
        Console.WriteLine(Proceed);
    }
    

    In Blazor Components we do not usually use constructors.

    When you do use them, they execute before the framework can set the Parameters and before DI can do property injection. So your WriteLine() is just running way too early.

    The code you have here will function properly in OnInitialized() and/or OnParametersSet().