Example 11
+Send values with HTMX
+Add request parameters declaratively without assembling JSON by hand
+hx-val-* and hx-all-vals
+ Both forms send values with the request and create an hx-vals attribute.
diff --git a/README.md b/README.md
index 3ab9555..a5e86eb 100644
--- a/README.md
+++ b/README.md
@@ -582,6 +582,30 @@ you can assign them to the `hx-all-headers` attribute:
The `HtmxHeaderTagHelper` will take care of all the remaining work regarding JSON serialization and escaping.
+### HtmxValsTagHelper
+
+The `HtmxValsTagHelper` adds values that HTMX includes with a request. Use `hx-val-*`
+attributes instead of writing JSON manually:
+
+```html
+
+```
+
+The following HTML will be generated:
+
+```html
+
+```
+
+You can also provide the values as a dictionary with the `hx-all-vals` attribute.
+
### HtmxRequestTagHelper
The `HtmxRequestTagHelper` configures the htmx request options supported by HTMX 1.x and 2.x.
diff --git a/samples/Ramstack.HtmxToolkit.Demo/Pages/Examples/HtmxVals.cshtml b/samples/Ramstack.HtmxToolkit.Demo/Pages/Examples/HtmxVals.cshtml
new file mode 100644
index 0000000..3eb8838
--- /dev/null
+++ b/samples/Ramstack.HtmxToolkit.Demo/Pages/Examples/HtmxVals.cshtml
@@ -0,0 +1,45 @@
+@page
+@model HtmxValsModel
+@{
+ ViewData["Title"] = "HTMX values";
+}
+
+ Example 11 Add request parameters declaratively without assembling JSON by hand Both forms send values with the request and create an Send values with HTMX
+
+ hx-val-* and hx-all-valshx-vals attribute.
Category: {Encode(category)}
+Format: {Encode(format)}
+ """); + + static string Encode(string? text) => + text is { Length: > 0 } + ? HtmlEncoder.Default.Encode(text) + : "(not supplied)"; + } +} diff --git a/samples/Ramstack.HtmxToolkit.Demo/Pages/Index.cshtml b/samples/Ramstack.HtmxToolkit.Demo/Pages/Index.cshtml index 54bff4d..691dc25 100644 --- a/samples/Ramstack.HtmxToolkit.Demo/Pages/Index.cshtml +++ b/samples/Ramstack.HtmxToolkit.Demo/Pages/Index.cshtml @@ -19,5 +19,6 @@Detect links enhanced by HTMX.
Request and swap a small response.
Post a protected form via HTMX.
+Send additional request values declaratively.
diff --git a/samples/Ramstack.HtmxToolkit.Demo/Pages/Shared/_Layout.cshtml b/samples/Ramstack.HtmxToolkit.Demo/Pages/Shared/_Layout.cshtml index d98f437..e57bd1f 100644 --- a/samples/Ramstack.HtmxToolkit.Demo/Pages/Shared/_Layout.cshtml +++ b/samples/Ramstack.HtmxToolkit.Demo/Pages/Shared/_Layout.cshtml @@ -38,6 +38,7 @@ 08. Boosted navigation 09. On-demand content 10. Antiforgery + 11. HTMX values diff --git a/src/Ramstack.HtmxToolkit/TagHelpers/HtmxHeaderJsonSerializerContext.cs b/src/Ramstack.HtmxToolkit/TagHelpers/HtmxDictionaryJsonSerializerContext.cs similarity index 74% rename from src/Ramstack.HtmxToolkit/TagHelpers/HtmxHeaderJsonSerializerContext.cs rename to src/Ramstack.HtmxToolkit/TagHelpers/HtmxDictionaryJsonSerializerContext.cs index b780e5a..ae4ce45 100644 --- a/src/Ramstack.HtmxToolkit/TagHelpers/HtmxHeaderJsonSerializerContext.cs +++ b/src/Ramstack.HtmxToolkit/TagHelpers/HtmxDictionaryJsonSerializerContext.cs @@ -4,4 +4,4 @@ namespace Ramstack.HtmxToolkit.TagHelpers; [JsonSerializable(typeof(IDictionary