Click here to Skip to main content
15,892,965 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Currently, I have a blazor component that creates a dropdown menu where the user selects a rack and socket. Once, the rack and socket are chosen, the code then adds the selection to a new object. However, I also want the device that they are choosing the rack and socket for to be also added to the object.

This is my current code:
 @foreach (var device in Devices)
{
    <tr>
        <td>@device.DeviceId</td>
        <td>
            <select class="rack" @onchange="NonBulkLocationSelected">
                <option id="default1">Select A Rack</option>
                @foreach (var rack in availableLocations)
                {
                    <option value="@rack.LocalId0">@rack.LocalId0</option>
                }
            </select>
        </td>
        <td>
            <select class="socket" @onchange="NonBulkLocationSelected">
                <option id="default1">Select A Socket</option>
                @foreach (var socket in availableLocations)
                {
                    <option value="@socket.LocalId1">@socket.LocalId1</option>
                }
            </select>
        </td>
    </tr>
}


The on-change event that is called assigns the dropdown menu's value to a variable that is later stored in in a new object. To accomplish that, I used LINQ queries like:
selectedRack = availableLocations.Where(x => x.LocalId0.ToString().Equals(e.Value)).FirstOrDefault();
(e is eventArgs

However, I don't know of a LINQ query that would be able to get me the device as well and have that stored in a variable. Can someone help me out with that?

What I have tried:

selectedDevice = Devices.Where(x => x.Device.ToString().Equals(e.Value)).FirstOrDefault();

But this obviously did not work since I know very little about LINQ queries.
Posted
Updated 16-Dec-20 11:34am
v2

1 solution

You can pass the device object directly as part of your @onchange event using a Lambda expression

<select class="rack" @onchange="(e)=>NonBulkLocationSelected(e, device)">


in code you then have

protected void NonBulkLocationSelected(ChangeEventArgs e, Device device) 
{
var selecteditem = e.Value; //this will be a string
// do something with device
}


The @onchange event produces a ChangeEventArgs object. You capture this as "e" and pass it into the call to event handler along with your device object.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900