Blazor - drop down onchange or onclick events not firing
Asked Answered
C

1

7
 <MudTd Style="text-align: right" DataLabel="Severity">
                <MudSelect Dense="true" T="string" Label="Severity" Variant="Variant.Filled"  Placeholder="Please Select" Required="true" @bind-Value="rcontext.Severity" @onchange="DoStuff">
                    <MudSelectItem @onchange="DoStuff"  Value="@("Acceptable")"/>
                    <MudSelectItem Value="@("Tolerable")" />
                    <MudSelectItem Value="@("Undesirable")" />
                    <MudSelectItem Value="@("Intolerable")" />
                </MudSelect>
         </MudTd>

   void DoStuff(ChangeEventArgs e)
    {
        string selectedString = e.Value.ToString();
        Console.WriteLine("It is definitely: " + selectedString);
    }

I am not able to invoke this function at all DoStuff() I have put in MudSelect and MudSelectItem tags, but none of the onchange or onclick events are getting fired.

Am I missing something.

whole code

@using System.Net.Http.Json
@using BlazorApp4.Shared.Entities
@using BlazorApp4.Client.Extensions
@inject HttpClient httpClient

<style>
    .mud-table-cell-custom-group {
        font-weight: 500;
    }

    .mud-table-cell-custom-group-footer {
        padding-bottom: 50px;
        text-align: right;
    }
</style>
<br />
<EditForm OnSubmit="somefunc" Model="@Elements">
<MudTable  Breakpoint="Breakpoint.Sm" Height="700px"  
          Items="@Elements"
          GroupBy="@_groupDefinition"
          GroupHeaderStyle="@($"background:{Colors.Grey.Lighten4};")"
          GroupFooterClass="mb-4"
          Dense="true"
          Hover="true"
          HorizontalScrollbar="true"
          Style="width:100%"
          Striped ="false"
          
          @bind-SelectedItem="selectedItem1">
    <ColGroup>
        <col style="width: 5px;" />
        <col  style="width:30%;"/>
        <col  style="width:15%;"/>
        <col  style="width:25%;"/>
        <col  style="width:15%;"/>
        <col  style="width:15%;"/>
        

    </ColGroup>
    <HeaderContent>
       
        <MudTh><h6><b>Identified Risk</b></h6></MudTh>
        <MudTh><h6><b>Yes/No/NA</b></h6></MudTh>
        <MudTh><h6><b>Comments & Discussion</b></h6>    </MudTh>
        <MudTh><h6><b>Likelihood</b>    </h6></MudTh>
        <MudTh><h6><b>Severity</b></h6></MudTh>

          


    </HeaderContent>
    <GroupHeaderTemplate Context="gcontext">
        @*working code -   <MudTh Class="mud-table-cell-custom-group"   colspan="5"><b>@($"{context.GroupName}##{context.Key}##{((context.Items.Sum((e) => e.Rating) >= 5 ) ? "High" : "Low")} ")</b></MudTh> *@
        <MudTh Class="mud-table-cell-custom-group"   colspan="5"><b>@($"{gcontext.GroupName}##{gcontext.Key}##{gcontext.Items.Syngenta<Questionnaire>(":")} ")</b></MudTh>
    </GroupHeaderTemplate> 
    <RowTemplate Context="rcontext">
        @if (true)
        {
            <MudTd  DataLabel="Identified Risk">@((MarkupString)rcontext.IdentifiedRisk)</MudTd>
        }
       <MudTd Style="text-align: right" DataLabel="Yes/No/NA">
                <MudSelect Dense="true" T="string" Label="Answer" Variant="Variant.Filled"  Placeholder="Please Select" Required="true" @bind-Value="rcontext.Yes_No_NA">
                    <MudSelectItem  Value="@("Yes")" />
                    <MudSelectItem Value="@("No")" />
                    <MudSelectItem Value="@("N/A")" />
                   
                </MudSelect>
         </MudTd>
        <MudTd DataLabel="Comments & Discussion">
            @rcontext.Comments_Discussion
            
         </MudTd>
        <MudTd Style="text-align: right" DataLabel="Likelihood">
                <MudSelect Dense="true" T="string" Label="Likelihood" Variant="Variant.Filled"  Placeholder="Please Select" Required="true" @bind-Value="rcontext.Likelihood">
                    <MudSelectItem  Value="@("Improbable")" />
                    <MudSelectItem Value="@("Possible")" />
                    <MudSelectItem Value="@("Probable")" />
                   
                </MudSelect>
         </MudTd>
<MudTd Style="text-align: right" DataLabel="Severity">
    <MudSelect Dense="true" T="string" Label="Severity" Variant="Variant.Filled" Placeholder="Please Select" Required="true" 
               Value="rcontext.Severity" ValueChanged="value => DoStuff(rcontext, value)">
        <MudSelectItem Value="@("Acceptable")" />
        <MudSelectItem Value="@("Tolerable")" />
        <MudSelectItem Value="@("Undesirable")" />
        <MudSelectItem Value="@("Intolerable")" />
    </MudSelect>
</MudTd>
         
@code {
    void DoStuff(YourContextClass  context, string selectedString)
    {
           context.Severity = selectedString;
         Console.WriteLine("It is definitely: " + selectedString);
    }
}


         
    </RowTemplate>

</MudTable>
  <MudButton ButtonType="ButtonType.Submit" Variant="Variant.Filled" Color="Color.Primary" Class="ml-auto">Register Request and Continue</MudButton>

  </EditForm>
@selectedItem1.Likelihood
@*<MudText Inline="true" Class="align-self-center">Selected1: @selectedItem1?.Concati()</MudText>*@


@code {

    [Parameter]
    public string? QuestionnaireFileName { get; set; }

    private TableGroupDefinition<Questionnaire> _groupDefinition = new()
    {
        GroupName = "Group",

        Indentation = false,
        Expandable = true,

        IsInitiallyExpanded = true,

        Selector = (e) => e.GroupBy
    };

           void DoStuff(string  selectedString)
    {
        
        Console.WriteLine("It is definitely: " + selectedString);
    }
    Questionnaire selectedItem1 = new Questionnaire();


    private IEnumerable<Questionnaire>? Elements = new List<Questionnaire>();

    void somefunc(EditContext ed)
    {
        StateHasChanged();
    }

    void DoStuffDup()
    {
        
        Console.WriteLine("It is definitely duplicate: ");
    }
   
    protected override async Task OnInitializedAsync()
    {
        
        Elements = await httpClient.GetFromJsonAsync<List<Questionnaire>>($"api/questions/{QuestionnaireFileName}");
    }

   
   
}
Conchitaconchobar answered 14/9, 2022 at 6:16 Comment(1)
doesnot work manConchitaconchobar
P
11

@bind-Value="rcontext.Severity" syntax handles the EventCallback automatically and assigns the selected value to rcontext.Severity.

Instead, you can use manual event binding:

Value="rcontext.Severity" ValueChanged="value => HandleSeverityChanged(rcontext, value)"

and handle the EventCallback manually:

void HandleSeverityChanged(Questionnaire context, string value)
{
     context.Severity = value;
}

So for your scenario your code should look like this:

<MudTd Style="text-align: right" DataLabel="Severity">
    <MudSelect Dense="true" T="string" Label="Severity" Variant="Variant.Filled" Placeholder="Please Select" Required="true" 
               Value="rcontext.Severity" ValueChanged="value => DoStuff(rcontext, value)">
        <MudSelectItem Value="@("Acceptable")" />
        <MudSelectItem Value="@("Tolerable")" />
        <MudSelectItem Value="@("Undesirable")" />
        <MudSelectItem Value="@("Intolerable")" />
    </MudSelect>
</MudTd>
         
@code {
    void DoStuff(Questionnaire context, string selectedString)
    {
         context.Severity = selectedString;
         Console.WriteLine("It is definitely: " + selectedString);
    }
}
Protrusile answered 14/9, 2022 at 7:45 Comment(2)
Hey this rcontext is an context object and DoStuff function doesnot have access to tcontext object.Conchitaconchobar
You can pass the context object to your DoStuff method. Updated my answer with example.Protrusile

© 2022 - 2024 — McMap. All rights reserved.