Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blazor and HMTX: form only get #3065

Open
enrico-rossini-tracebit opened this issue Dec 9, 2024 · 3 comments
Open

Blazor and HMTX: form only get #3065

enrico-rossini-tracebit opened this issue Dec 9, 2024 · 3 comments

Comments

@enrico-rossini-tracebit
Copy link

enrico-rossini-tracebit commented Dec 9, 2024

I have a project in .NET8 for ASP.NET Core. This project has HTMX. Now, I want to add Blazor to the solution. I have quite a simple page that mimics the example on GitHub.

<CascadingValue Value="ValidationResult">
    <form id="counter" hx-post="test/ui" hx-swap="outerHTML" 
        hx-target="#counter">
        <HtmxAntiforgeryToken/>
        <input type="hidden" id="check" value="@SlackForm.Check" />
        <button class="btn btn-primary">Click me</button>
    </form>
</CascadingValue>

@code {
    [Parameter] public UIModel TestForm { get; set; } = new();
    [Parameter] public ValidationResult ValidationResult { get; set; } = new();
}

I added in the Program.cs the minimal APIs for this page. The issue is that every time I click the button, the request from the form is to the GET. There is no way to send a request as a POST.

Do you have any idea if this is related to a configuration of the project or something related to the HTMX working with Blazor?

@Telroshan
Copy link
Collaborator

Hey, what URL do you see in your network inspector for that GET request ?

If the request is made to the current page, then it probably means htmx hasn't had the chance to initialize your form at all (making it therefore fallback to the native default behavior, i.e. GET request against the current page URL)

If that's the case:

  • it could be as simple as the htmx script not being included on your page at all (make sure it is loaded)
  • if those elements aren't rendered in the initial server response's HTML content, and it's instead any frontend library that appends those elements to the page, you'll likely want to use htmx.process to get htmx to correctly initialize such elements (also see the 3rd party javascript section of the docs)

If that's not the case, please provide more information about that request (ideally, provide the whole request data, with headers and payload)

Hope this helps!

@benjaminsampica
Copy link

benjaminsampica commented Dec 14, 2024

If you’re adding Blazor interactivity (Blazor.web.js) to the example, one thing to keep in mind is that Blazor will by default send fetch requests (enhanced navigation just like HTMX) to same-origin endpoints via GET, unless configured not to do so - manually opting out of enhanced navigation or building a Blazor-enhanced form.

This is probably intercepting the behavior you’re expecting out of HTMX.

If this isn’t enough, as Telroshan said, some additional details would help.

@nickpreston24
Copy link

nickpreston24 commented Dec 18, 2024

@enrico-rossini-tracebit If all else fails, and you give up, I suggest adding a combination of AlpineJS and Hydro to a new Razor project.

I'm assuming this project is brand new.

Benefits of Razor x HTMX&Hydro + Alpine ('RazorHAT'):

  • Faster Hot Reloads than Blazor
  • YOU control WHEN requests render, not Blazor
  • Pushes you to learn HTMX and AlpineJS and maximize Razor views knowledge.
  • You can make your own HydroViews and html tags, which can replace .razor components, while retaining Razor's Partials, Views and ViewComponents - all of which have their own niches and strengths.

Drawbacks:

  • You have to figure out how these 3-4 pieces of tech can work together (but once you do, you'll be flying so fast, it won't matter)

  • You won't have Mommy Microsoft trying to coddle you (a plus for me)

  • YOU suffer early on, but you also set the pace once you realize YOU are the "framework", not Blazor, not React or anything else that tries to solve the problem, but ends up becoming a paradigm or a cult, instead of what you needed in the first place (oh God, what have we done to this industry!?)

I've gone thru learning Razor Pages, and it's really, really nice to have my project (re)load in < 250 ms in Hot Reload no matter how many nugets or random JS libs I use.

If you want tips on how to do this, feel free to ask. Otherwise, I recommend searching htmx on Khalid A's blog, or starting with this guide, here

Good luck, and have fun.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants