Working with Forms

Handling form submissions and validation.

Form Basics

Forms in Swap.Htmx use standard ASP.NET Core Tag Helpers combined with HTMX attributes.

<form hx-post="@Url.Action("Save")" hx-swap="outerHTML">
    <input asp-for="Name" />
    <span asp-validation-for="Name"></span>
    <button type="submit">Save</button>
</form>

Validation

Server-side validation works standardly. If ModelState is invalid, simply return the view with the model.

[HttpPost]
public IActionResult Save(MyModel model)
{
    if (!ModelState.IsValid)
    {
        return SwapView(model);
    }
    
    // Save logic...
    return SwapView("Success", model);
}