Table of Contents

Class HtmxLocationOptions

Namespace
Ramstack.HtmxToolkit
Assembly
Ramstack.HtmxToolkit.dll

Represents the options for an HX-Location request.

public sealed class HtmxLocationOptions
Inheritance
HtmxLocationOptions
Inherited Members

Examples

[HttpPost]
public IActionResult Save(ProductInput input)
{
    var product = catalog.Update(input);

    // Load the details fragment into the page, but show the product page URL
    // in the address bar so that a reload or a shared link opens the full page.
    Response.Htmx(htmx => htmx.Location(
        Url.Action(nameof(Details), new { id = product.Id })!,
        new HtmxLocationOptions
        {
            Target = "#product-details",
            Swap = HtmxSwap.OuterHtml,
            HistoryUrl = Url.Action(nameof(Show), new { id = product.Id })
        }));

    return NoContent();
}

Properties

Headers

Gets or sets the headers to include with the request.

public IDictionary<string, string>? Headers { get; set; }

Property Value

IDictionary<string, string>

Remarks

Supported in HTMX 1.9.x, HTMX 2.x, and HTMX 4.x.

Header values must be strings. Pass complex data as a pre-serialized JSON string.

HistoryAction

Gets or sets how the browser history is updated. The default is Push.

public HtmxHistoryAction HistoryAction { get; set; }

Property Value

HtmxHistoryAction

HistoryUrl

Gets or sets the URL used to update the browser history. If null, the URL of the loaded page is used.

public string? HistoryUrl { get; set; }

Property Value

string

Remarks

Ignored when HistoryAction is None. Custom history URLs are supported in HTMX 2.0.8 and later 2.x releases, and in HTMX 4.x.

Select

Gets or sets a selector used to filter the content to swap from the response.

public string? Select { get; set; }

Property Value

string

Remarks

Supported in HTMX 1.9.x, HTMX 2.x, and HTMX 4.x.

SelectOob

Gets or sets a comma-separated list of elements, each optionally followed by a colon and a swap style, used to select content for out-of-band swaps from the response.

public string? SelectOob { get; set; }

Property Value

string

Remarks

Supported in HTMX 2.0.8 and later 2.x releases, and in HTMX 4.x.

HTMX 2.x accepts only element IDs, with or without a leading #, for example #alerts,#cart:innerHTML. HTMX 4.x accepts CSS selectors. Neither version supports IDs or selectors that contain a comma or a colon, such as li:first-child.

Source

Gets or sets the CSS selector for the element used as the source of the new request.

public string? Source { get; set; }

Property Value

string

Remarks

Supported in HTMX 1.9.x, HTMX 2.x, and HTMX 4.x.

Swap

Gets or sets how the response will be swapped relative to the target element.

public HtmxSwap? Swap { get; set; }

Property Value

HtmxSwap?

Remarks

Supported in HTMX 1.9.x, HTMX 2.x, and HTMX 4.x.

Reads the style from SwapExpression, or returns null if the style is not recognized. Assigning a style replaces the complete expression, including any modifiers. Assigning null clears the swap.

SwapExpression

Gets or sets the complete swap expression, including any swap modifiers.

public string? SwapExpression { get; set; }

Property Value

string

Remarks

Supported in HTMX 1.9.x, HTMX 2.x, and HTMX 4.x.

Target

Gets or sets the selector for the target element into which the response will be swapped.

public string? Target { get; set; }

Property Value

string

Remarks

Supported in HTMX 1.9.x, HTMX 2.x, and HTMX 4.x.

Values

Gets or sets the form field values to submit with the request.

public IDictionary<string, HtmxFieldValues>? Values { get; set; }

Property Value

IDictionary<string, HtmxFieldValues>

Examples

// After archiving, reload the product list with its filters applied.
Response.Htmx(htmx => htmx.Location(Url.Action("Index", "Products")!, new HtmxLocationOptions
{
    Target = "#product-list",
    Values = new Dictionary<string, HtmxFieldValues>
    {
        ["status"] = "archived",          // One value.
        ["category"] = ["books", "music"] // Several values of one field.
    }
}));

Remarks

Supported in HTMX 1.9.x, HTMX 2.x, and HTMX 4.x.

HTMX 1.9.x and 2.x submit array values as repeated parameters. HTMX 4.0.0 converts an array to a single comma-separated field value.