From b3801355002707feb7bae617ca8dd20d2720c151 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ra=20El-Saig?= Date: Thu, 9 Nov 2023 16:35:12 +0100 Subject: [PATCH 1/3] Fix bug in SetItems extension method. --- .../Extensions/CollectionExtensions.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Lombiq.HelpfulLibraries.Common/Extensions/CollectionExtensions.cs b/Lombiq.HelpfulLibraries.Common/Extensions/CollectionExtensions.cs index 0217ae31..03c8e336 100644 --- a/Lombiq.HelpfulLibraries.Common/Extensions/CollectionExtensions.cs +++ b/Lombiq.HelpfulLibraries.Common/Extensions/CollectionExtensions.cs @@ -42,6 +42,10 @@ public static void RemoveAll(this ICollection collection, Func pr /// public static void SetItems(this ICollection collection, IEnumerable newValues) { + // If the two variables point to the same object, nothing should be done. Otherwise this would clear the + // collection and then add nothing to it. + if (Equals(collection, newValues)) return; + collection.Clear(); collection.AddRange(newValues); } From d651eb45716f6d1de8131bc57b734bc716c679d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ra=20El-Saig?= Date: Thu, 9 Nov 2023 16:35:39 +0100 Subject: [PATCH 2/3] Copy UpdateModelExtensions from OCC. --- .../Mvc/UpdateModelExtensions.cs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Lombiq.HelpfulLibraries.OrchardCore/Mvc/UpdateModelExtensions.cs diff --git a/Lombiq.HelpfulLibraries.OrchardCore/Mvc/UpdateModelExtensions.cs b/Lombiq.HelpfulLibraries.OrchardCore/Mvc/UpdateModelExtensions.cs new file mode 100644 index 00000000..7a9cfe6a --- /dev/null +++ b/Lombiq.HelpfulLibraries.OrchardCore/Mvc/UpdateModelExtensions.cs @@ -0,0 +1,18 @@ +using Microsoft.AspNetCore.Mvc.ModelBinding; +using System.Collections.Generic; +using System.Linq; + +namespace OrchardCore.DisplayManagement.ModelBinding; + +public static class UpdateModelExtensions +{ + public static IEnumerable GetModelErrors(this IUpdateModel updateModel) => + updateModel + .ModelState + .Values + .SelectMany(entry => entry.Errors) + .Where(error => !string.IsNullOrWhiteSpace(error.ErrorMessage)); + + public static IEnumerable GetModelErrorMessages(this IUpdateModel updateModel) => + updateModel.GetModelErrors().Select(error => error.ErrorMessage); +} From f9571f85f25befebaba4226e08ed9cee35005e62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ra=20El-Saig?= Date: Thu, 9 Nov 2023 16:48:25 +0100 Subject: [PATCH 3/3] Add docs. --- Lombiq.HelpfulLibraries.OrchardCore/Docs/Mvc.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Lombiq.HelpfulLibraries.OrchardCore/Docs/Mvc.md b/Lombiq.HelpfulLibraries.OrchardCore/Docs/Mvc.md index 9faa4ac3..8070ee90 100644 --- a/Lombiq.HelpfulLibraries.OrchardCore/Docs/Mvc.md +++ b/Lombiq.HelpfulLibraries.OrchardCore/Docs/Mvc.md @@ -5,6 +5,7 @@ - `OrchardControllerExtensions`: Adds extension methods like `RedirectToContentDisplay(content)` to `Controller` objects. - `ResultExecutingContextExtensions`: contains shortcuts for common context operations in `IAsyncResultFilter`s. - `ShapeResultExtensions`: Adds extensions methods generating placement strings on your shape description, such as `UseTab()`. +- `UpdateModelExtensions`: Adds extension methods for working with the `IUpdateModel` such as `GetModelErrors()`. - `UrlHelperExtensions`: Adds extension methods to the `@Url` helper, such as `EditContentItemWithTab()`. ## `TypedRoute`