N
forms form

Form

Alpine AJAX form wrapper.

$ ndui add form
Component docs

Alpine AJAX form wrapper

Source
@* Form pre-wires x-target (success), x-target.422 (validation errors),
   x-target.away="_top" (redirect → full reload). *@
<Form Id="product_form"
      OnSubmitTargetId="products_table"
      asp-page-handler="Save">
    <FormField asp-for="Name" />
    <FormField asp-for="Price" />
    <FormField asp-for="Description" AsTextarea="true" />
    <Button type="submit">Save product</Button>
</Form>

@* In the PageModel — return 422 on validation error, 200 with partial on success *@
public IActionResult OnPostSave(ProductEditModel m)
{
    if (!ModelState.IsValid)
        return this.PageOrPartial("_ProductForm", m, UnprocessableEntity);

    _products.Save(m);
    return this.PageOrPartial("_ProductsTable", _products.List());
}