sayef4
sayef4

Reputation: 113

Changing the value of a set field in Blazor

Is there a way to manually change the value of a field if it has been assigned a value?

for example, if var numberofcars = 5 and is set to a text field to show the 5, is there way to overwrite that textfield to use your own value?

    <RowEditingTemplate>
    @{
    context.Total=context.Quantity*20;
    }
    
      <MudTd Style="text-align:center" Class="pa-0" DataLabel="Total">
                <MudNumericField Style="font-weight:bold; text-align:center;" on Format="0.0" @bind-Value="@context.Total" Required="true" RequiredError="" />
            </MudTd>
    
      <MudTd Style="text-align:center" Class="pa-0" DataLabel="Quantity">
                <MudNumericField Style="font-weight:bold; text-align:center;" on Format="0.0" @bind-Value="@context.Quantity" Required="true" RequiredError="" />
            </MudTd>
</RowEditingTemplate>

In the code above, I make context.Total equal to context.Quantity*20 so if the user entere quantity 1, the total will become 20. What I want to find out is if there is a way to change that set value to anything else in the total field?

Upvotes: 1

Views: 818

Answers (1)

Dimitris Maragkos
Dimitris Maragkos

Reputation: 11392

Here is what you can do:

<RowEditingTemplate>
    <MudTd Style="text-align:center" Class="pa-0" DataLabel="Total">
        <MudNumericField Style="font-weight:bold; text-align:center;" on Format="0.0" @bind-Value="@context.Total" Required="true" RequiredError="" />
    </MudTd>
    
    <MudTd Style="text-align:center" Class="pa-0" DataLabel="Quantity">
        <MudNumericField T="int" Style="font-weight:bold; text-align:center;" on Format="0.0" Value="@context.Quantity" ValueChanged="(value) => HandleQuantityChanged(context, value)" Required="true" RequiredError="" />
    </MudTd>
</RowEditingTemplate>

@code {
    // Change YourContextClass with the type of your own data
    private void HandleQuantityChanged(YourContextClass context, int value)
    {
        context.Quantity = value;
        context.Total = value * 20;
    }
}

When quantity changes total is updated to quantity * 20 but you can just set any value to total "manually".

Upvotes: 1

Related Questions