Mudblazor datagrid with Mudbutton onclick with parameter

DjGrego

Member
Joined
Nov 21, 2021
Messages
23
Programming Experience
Beginner
If I have a button in a Mudblazor datagrid with a Mudbutton in a column. I can run a method on that button such as onclick="Test". But how do I pass a parameter into the method showing which row the button was pressed? I would assume it would be something like this:


C#:
<MudDataGrid Items="@employees" Filterable="false" SortMode="@SortMode.None" Groupable="false">
    <Columns>
        <PropertyColumn Property="x => x.Name" />
        <TemplateColumn CellClass="d-flex justify-end">
            <CellTemplate>
                <MudStack Row>
                    <MudButton @onclick="@(x => Test(@x.Name))" Size="@Size.Small" Variant="@Variant.Filled" Color="@Color.Primary">Hire</MudButton>
                </MudStack>
            </CellTemplate>
        </TemplateColumn>
    </Columns>
</MudDataGrid>

@code {
         private void Test(string stuff)
        {
            Console.WriteLine(stuff);            //SoundIds.Add(await Howl.Play(file));
        }
}

The onclick is not working. Can someone point me in the right direction, please?

Thanks,
Greg
 
What happens when you use the traditional Blazor way of doing parameters?
C#:
@onclick="() => Test(x.Name)"
 
Indeed, as SD indicates, the delegate you provide to OnClick has to adapt between what the button wants (either an invokable member with no arguments or an invokable member that takes a MouseEventArgs argument) and what you want to run (an invokable member Test with one string argument)

OnClick="_ => Test(x.Name)" is an alternative form
 
MudBlazor @onclick:
@onclick="() => Test(context.Item.Name)"
ended up working for me (though I don't understand why. :unsure:

In both of your suggestions, VS didn't know what x was. ( I would think it should )
 
On a related note. I'm sending another part of the object to a audio player, but not working. If I send src="/audio/test.mp3" using audio in the wwwroot folder it will play, but my app will not play audio outside of the wwwroot folder. I can access data from this outside folder ("D:\data\audio\" which is correctly populating the datagrid for me, but the audio player will not access it.

I have googled "Blazor play audio" but they all seem to show how to play audio from the wwwroot folder. Any suggestions?

Blazor audio:
<audio controls autoplay>
    <source src="context.Item.Filepath" type="audio/mpeg" runat=server>
</audio>
 
In both of your suggestions, VS didn't know what x was. ( I would think it should )

We got it from you; both your xxxColumn tags said x.Name, and I didn't check that you'd renamed your context (you hadn't)

If you don't choose a specific name for a context variable, it's called context, so a MudTable that has Items=myEmployeeList will have (when laying out row data for example) an @context that is a single employee from the list, and then it has properties like normal

Any suggestions?

Worker process hosting the app doesn't have access permissions to the folder..
 
Back
Top Bottom