Radzen Dynamically Created Tabs - How to get a value of selected/active tab?

Hello again,

In my project, I created RadzenTabsItems based on VendorId of order details. I want to get the active/selected tab VendorId so that I can pass it to a method and use it. Any ideas on how I can solve this?

...
<RadzenTabs>
     <Tabs>

     @{
             var detailVendorId = 0;
       }
      @foreach (var detail in orderDetails)
       {
                @if (detailVendorId != detail.VendorId)
                {
                      <RadzenTabsItem Text="@detail.Vendor.Name">
...

You will need some dictionary where you can access value by tab index. You can also bind SelectedIndex and use it to get desired value from the dictionary:

1 Like

Thank you @enchev. I used the dictionary as you suggested and it works but why it renders twice when the page loads? It gets my data doubled.

The vendors dictionary:

Here is how I added the dictionary:

...
<RadzenTabs @bind-SelectedIndex=@_selectedTab Change=@OnChange>
                        <Tabs>

                            @{
                                var detailVendorId = 0;
                               
                            }
                            @foreach (var detail in orderDetails)
                            {
                                @if (detailVendorId != detail.VendorId)
                                {
                                    <RadzenTabsItem Text="@detail.Vendor.Name">
...
                           </RadzenTabsItem>
                                    vendors.Add(count, detail.VendorId);
                                    count++;
      }
 int _selectedTab = 0;
 IDictionary<int, int> vendors = new Dictionary<int, int>();
 int count = 0;
 void OnChange(int index)
 {
     _selectedTab = index;
 }

Iā€™m afraid that I cannot comment since I know nothing about your application. Maybe you have server prerender:

Whatever is causing this you can simply check if the key is already present before adding.

1 Like