htmx samples
htmx reads hx-* attributes, makes a request, and swaps the returned HTML into the page. Every sample here calls a
named handler on this page's model (?handler=…), which returns a partial: a fragment of HTML, not a whole page and not JSON.
All the logic stays in C#; there's no JavaScript of our own on this page.
1. Load on click
hx-get calls OnGetServerTime; hx-target says where the returned HTML goes.
2. Live search
hx-trigger="input changed delay:300ms" waits until you pause typing, then calls OnGetSearch with the box's value. hx-indicator shows "Searching…" while the request runs.
- Acme Plumbing
- Bright HVAC
- Cedar Roofing
- Delta Electric
- Evergreen Landscaping
- Fairway Plumbing & Drains
3. Form without a page reload
hx-post sends the form to OnPostGreet, and hx-swap="outerHTML" replaces the whole form with the returned one: with errors if invalid, or a greeting if valid. The anti-forgery token is a hidden field the form tag helper adds, so htmx sends it along automatically.
4. Polling
hx-trigger="every 5s" re-asks OnGetLiveCalls every five seconds: how a dashboard could show calls in progress.