1

I am creating an app in Blazor/C#, using mostly Radzen components. My goal is to have a user select the number of items to add, using RadzenNumeric, and then dynamically create that number of text boxes.

I found a working example, that almost fits my needs, with . but, I can't seem to get it to fire from the RadzenNumeric Change="@onchange_n".

Here is what I had so far:

<RadzenNumeric Value="@N" class="w-100" Change="@onchange_n" />  Sum: @total

I feel that it's the Value attribute that I am getting wrong. I based it on the working example I found below. This one works:

<input type="number" value="@N" @oninput="onchange_n"> Sum: @total

Code:

@foreach (var i in Enumerable.Range(0,N))
{
var ii=i;
<input type="number" value="@MyList[ii]"
@oninput="(e) => resum(ii, e)"><br>
}


@code
{
private int N = 0, total = 0;
private List<int> MyList = new List<int>();
private Void onchange_n(ChangeEventArgs e)
{
N=0;
if (Int32.TryParse(e.Value.ToString(), out int n)) N = n
MyList.Clear();
MyList.AddRage(Enumerable.Range(0,N).Select(_=>0));
StateHasChanged();
}

private void resum(int i, ChangeEventArgs e)
{
var n = 0;
if (Int32.TryParse(e.Value.ToString(), out int auxn)) n = auxn;
MyList[i] = n;
total = MyList.Sum(x=>x);
}
}

I don't understand why it will fire onchange_n properly from the (<input type="number") box, but not the RadzenNumeric. is Value not the proper place for @N ? what am I missing.?

ExecChef
  • 387
  • 2
  • 13

1 Answers1

1

The type of input element's @oninput parameter is EventCallback<ChangeEventArgs> but the type of RadzenNumeric component's Change parameter is EventCallback<TValue>.

Try:

<RadzenNumeric TValue="int" Value="@N" class="w-100" Change="@onchange_n" />  Sum: @total

@code {
    private int N = 0, total = 0;
    private List<int> MyList = new List<int>();

    private void onchange_n(int value)
    {
        N = value;
        MyList.Clear();
        MyList.AddRange(Enumerable.Range(0, N).Select(_ => 0));
        StateHasChanged();
    }

    private void resum(int i, ChangeEventArgs e)
    {
        var n = 0;
        if (Int32.TryParse(e.Value.ToString(), out int auxn)) n = auxn;
        MyList[i] = n;
        total = MyList.Sum(x => x);
    }
}
Dimitris Maragkos
  • 8,932
  • 2
  • 8
  • 26
  • 1
    Thank you for explaining this. It helped me understand what was going wrong. Your solution worked. Thank you. – ExecChef Sep 27 '22 at 12:02