Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MudBlazor. How to pass a function as a modal parameter?

I tried passing and calling the function like this but Visual Studio tekk me: Converting method group 'OnOk' to non-delegate type 'object'. Did you intend to invoke the method?

My TestPage.razor

<div>
Some content..
</div>

@code {

    async Task DeleteServer(Server server)
    {
        var parameters = new DialogParameters { ["server"] = server, 
            ["Title"] = "Title Text !!!",
            ["ContentText"] = "Content text !!!",
            ["OkBtnText"] = "Окейси",
            ["OkButtonDisabled"] = true,
            ["OnOk"] = OnOk
        };

        var dialog = DialogService.Show<Modal>("Delete Server", parameters);
        var result = await dialog.Result;

        if (!result.Cancelled)
        {
            Guid.TryParse(result.Data.ToString(), out Guid deletedServer);
            Servers.RemoveAll(item => item.Id == deletedServer);
        }
    }

    private void OnOk()
    {
        Console.WriteLine("Ok Cliked");
    }

and Modal.razor

    <DialogActions>
        <MudButton Color="Color.Success" OnClick="Cancel">Cancel</MudButton>
        <MudButton OnClick=@HandleOkClicked>@OkBtnText</MudButton>
    </DialogActions>

@code {

    [Parameter]
    public EventCallback OnOk { get; set; }

    private void HandleOkClicked()
    {
        OnOk.InvokeAsync();
    }
}
like image 271
ru_cosmonaut Avatar asked Sep 18 '25 07:09

ru_cosmonaut


1 Answers

Try this:

var parameters = new DialogParameters
{
  ...
  ["OnOk"] = EventCallback.Factory.Create(this, OnOk)
};
like image 74
Bart Kiers Avatar answered Sep 19 '25 22:09

Bart Kiers