Skip to content

Commit

Permalink
Merge pull request #223 from Lombiq/issue/OCC-63
Browse files Browse the repository at this point in the history
OCC-63: Fix bug in SetItems extension method
  • Loading branch information
DemeSzabolcs authored Nov 10, 2023
2 parents d507bd8 + f9571f8 commit ffd7762
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ public static void RemoveAll<T>(this ICollection<T> collection, Func<T, bool> pr
/// </summary>
public static void SetItems<T>(this ICollection<T> collection, IEnumerable<T> 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);
}
Expand Down
1 change: 1 addition & 0 deletions Lombiq.HelpfulLibraries.OrchardCore/Docs/Mvc.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
18 changes: 18 additions & 0 deletions Lombiq.HelpfulLibraries.OrchardCore/Mvc/UpdateModelExtensions.cs
Original file line number Diff line number Diff line change
@@ -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<ModelError> GetModelErrors(this IUpdateModel updateModel) =>
updateModel
.ModelState
.Values
.SelectMany(entry => entry.Errors)
.Where(error => !string.IsNullOrWhiteSpace(error.ErrorMessage));

public static IEnumerable<string> GetModelErrorMessages(this IUpdateModel updateModel) =>
updateModel.GetModelErrors().Select(error => error.ErrorMessage);
}

0 comments on commit ffd7762

Please sign in to comment.