Refactor favorite button: wrap in form for better HTMX and AntiForgeryToken support

This commit is contained in:
Francesco Lorenzo D'Amico
2025-06-02 09:25:26 +02:00
parent 294f601085
commit 28e60b79fa
@@ -1,11 +1,11 @@
@model Francesco.Recipes.World.Models.BackendModels.Recipe.Recipe
<button class="btn btn-link p-0"
style="width: 40px; height: 40px;"
hx-post="@Url.Action(Model.IsFavorite ? "RemoveFavorite" : "AddFavorite", "Recipe")"
hx-vals='{"recipeId": "@Model.Id"}'
<form hx-post="@Url.Action(Model.IsFavorite ? "RemoveFavorite" : "AddFavorite", "Recipe")"
hx-target="this"
hx-swap="outerHTML">
@Html.AntiForgeryToken()
<input type="hidden" name="recipeId" value="@Model.Id" />
<button type="submit" class="btn btn-link p-0" style="width: 40px; height: 40px;">
@if (Model.IsFavorite)
{
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="#FFD700">
@@ -22,4 +22,5 @@
2 9.24l5.46 4.73L5.82 21z" />
</svg>
}
</button>
</button>
</form>