Search code examples
blazor

Pass 2 parameters to OnChange in a select from the elements


i got this select with a set of elements, an element is a description and a name.

i would like to get the

parameter.Name and element.Name

when there is a change in the selection. If i just use my SelectValueChange i just get the element.Name but that does not really help me enough. any ideas?

<select class="form-control" @onchange="SelectValueChange">
    @foreach (Element element in parameter.Domain.Elements)
    {
        <option [email protected]>@element.Description</option>
    }
</select>


@code
{
    public void SelectValueChange(ChangeEventArgs e) 
        {
            //how to know what select this is comming from
            need to know the parameter.Name value here.
        }
}

Solution

  • You can't bound to object, meaning you can't bound to an Element object like this: value=@element. You can only bound to string values.

    You may bound to element.Name which is string, like this:

    <option value="element.Name">@element.Description</option>
    

    Your SelectValueChange may look like this:

    private void SelectValueChange (ChangeEventArags args)
    {
          var selectedName = args.Value.ToString();
    }
    

    parameter.Name and element.Name

    What do you mean parameter.Name ?

    Please show more code, if you still need help...

    UPDATE

    i'm fine with binding to element.Name . but i need to know the 'name' of this select. Because i got like 50 on the page. so i need to know the specific select the call is comming from. –

    I'm not sure if I understand you... Do you mean that you've got 50 select elements on your page with a single SelectValueChange method, and you want to know from which select element your SelectValueChange method has been called? If so, the simplest way is to add another parameter identifying the select element. It can be done in various ways... The simplest way to do that I guess is as follows:

     @onchange="@((ChangedEventArgs args) => SelectValueChange(args, "Select1"))"
    

    And your SelectValueChange may look like this:

    private void SelectValueChange (ChangeEventArags args, string selectName)
    {
          var selectedName = args.Value.ToString();
    }