Search code examples
c#asp.netasp.net-coreblazormudblazor

Problem with sending a value from child component to parent component using MudDialog


I have a problem with sending data from a child component to a parent component while using MudBlazor.

(Child component) <OknoDialogowe_Zatwierdzenie_Wybor_Daty>

public class UserData
{
    public DateTime wybranadata { get; set; }
}


private void Submit() {

    var data = new UserData
        {
            wybranadata = WybranaData,
        };

    MudDialog.Close(DialogResult.Ok(data));
    //  Wykonaj?.Invoke(); 
 
}
void Cancel(){
    MudDialog.CancelAll();
    //Ukryj?.Invoke();
}

Parent Component

public class UserData
{
    public DateTime wybranadata { get; set; }

}
...

var dane = await DialogService.ShowAsync<OknoDialogowe_Zatwierdzenie_Wybor_Daty>("Confirm", parameters);

var wynik = await dane.Result;
if (!wynik.Cancelled)
{
    var wyswietl = wynik.Data as UserData;
    if ( wyswietl != null)
    {
        Console.WriteLine(wyswietl.wybranadata);
    }
    else
    {
        Console.WriteLine("empty :(");
    }
 ;

How can I send data using as a class from <OknoDialogowe_Zatwierdzenie_Wybor_Daty> to the main component? I still receive value "empty" :(


Solution

  • You need to use IDialogService.GetReturnValueAsync<T>() to return the UserData object from the dialog, instead of wynik.Data as UserData;

    if (!wynik.Cancelled)
    {
        var wyswietl = await dialog.GetReturnValueAsync<UserData>();
        if ( wyswietl != null)
        {
            Console.WriteLine(wyswietl.wybranadata);
        }
        else
        {
            Console.WriteLine("empty :(");
        }
    }