From c60b41af1481355096ef88aad80be54bddbe392b Mon Sep 17 00:00:00 2001 From: Bernhard Straub Date: Mon, 1 Jul 2024 16:30:20 +0200 Subject: [PATCH] Improve DialogResult extension with TryGet for data Add result.Data --- .../Extensions/RxMudBlazorLightExtensionsExtern.cs | 12 ++++++++++++ .../CRUD/CRUDItemAddOrUpdate.razor | 8 ++------ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/RxMudBlazorLight/Extensions/RxMudBlazorLightExtensionsExtern.cs b/RxMudBlazorLight/Extensions/RxMudBlazorLightExtensionsExtern.cs index e588409..26db6a1 100644 --- a/RxMudBlazorLight/Extensions/RxMudBlazorLightExtensionsExtern.cs +++ b/RxMudBlazorLight/Extensions/RxMudBlazorLightExtensionsExtern.cs @@ -9,5 +9,17 @@ public static bool OK([NotNullWhen(true)] this DialogResult? result) { return result is not null && !result.Canceled; } + + public static bool TryGet(this DialogResult? result, [NotNullWhen(true)] out T? data) + { + data = default; + + if (result is not null && !result.Canceled) + { + data = (T?)result.Data; + } + + return data is not null; + } } } diff --git a/RxMudBlazorLightTestBase/CRUD/CRUDItemAddOrUpdate.razor b/RxMudBlazorLightTestBase/CRUD/CRUDItemAddOrUpdate.razor index a138f2b..33c7918 100644 --- a/RxMudBlazorLightTestBase/CRUD/CRUDItemAddOrUpdate.razor +++ b/RxMudBlazorLightTestBase/CRUD/CRUDItemAddOrUpdate.razor @@ -25,13 +25,9 @@ else var dialog = await DialogService.ShowAsync(AddMode ? "Add ToDo" : "Edit ToDo", parameters); var result = await dialog.Result; - if (result.OK()) + if (result.TryGet(out var scope)) { - var scope = (CrudService.CrudItemInput?)result.Data; - if (scope is not null) - { - await scope.SubmitAsync(); - } + await scope.SubmitAsync(); } } }