-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from bingzer/main
Adding Code Examples for <form> and Response.Htmx() in Htmx.Net
- Loading branch information
Showing
7 changed files
with
139 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
@page | ||
@model FormsModel | ||
@{ | ||
ViewData["Title"] = "Forms"; | ||
} | ||
|
||
<h6> | ||
Demonstrate <code><form></code> usage. | ||
</h6> | ||
<form hx-post hx-page="./Forms" hx-indicator="#login-indicator"> | ||
<p> | ||
For username, you may input any username. <br/> | ||
For password any text will get you in, except the word <code>password</code> | ||
</p> | ||
|
||
<div> | ||
Username: <input asp-for="Username"/> <br/> | ||
Password: <input asp-for="Password" type="password"/> | ||
</div> | ||
|
||
<div> | ||
<p id="login-indicator" class="htmx-indicator">Logging in...</p> | ||
|
||
<button type="submit">Login</button> | ||
</div> | ||
|
||
@Html.AntiForgeryToken() | ||
</form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.RazorPages; | ||
using System.ComponentModel.DataAnnotations; | ||
using Htmx; | ||
|
||
namespace Sample.Pages | ||
{ | ||
public class FormsModel : PageModel | ||
{ | ||
[BindProperty] | ||
[Required, Display(Name = "Username", Prompt = "(Any username will do)")] | ||
public string? Username { get; set; } | ||
|
||
[BindProperty] | ||
[Required, Display(Name = "Password", Prompt = "(Anything but the word password)")] | ||
public string? Password { get; set; } | ||
|
||
public async Task<IActionResult> OnPost() | ||
{ | ||
// simulating waiting... | ||
await Task.Delay(1000); | ||
|
||
var isValid = Password != "password"; | ||
|
||
if (!isValid) | ||
{ | ||
return Content("Username or password is wrong", "text/html"); | ||
} | ||
else | ||
{ | ||
return Content($"Welcome {Username}!", "text/html"); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
@page | ||
@model ResponseModel | ||
@{ | ||
ViewData["Title"] = "Response.Htmx()"; | ||
} | ||
|
||
<h6> | ||
Demonstrate <code>Response.Htmx(headers => headers.WithTrigger())</code> usage. | ||
</h6> | ||
<div> | ||
<p> | ||
|
||
</p> | ||
|
||
<form hx-post hx-page="./Response" hx-swap="none" hx-indicator="#sending-indicator"> | ||
<textarea asp-for="Message"></textarea> | ||
<br/> | ||
|
||
<p id="sending-indicator" class="htmx-indicator">Sending...</p> | ||
<button type="submit"> | ||
Send to server | ||
</button> | ||
</form> | ||
</div> | ||
|
||
<script type="text/javascript"> | ||
window.addEventListener("showMessage", evt => { | ||
let message = evt.detail.message; | ||
alert('This event is triggered by the server to client side. Message: ' + message) | ||
}); | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using Htmx; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.RazorPages; | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace Sample.Pages | ||
{ | ||
public class ResponseModel : PageModel | ||
{ | ||
[BindProperty] | ||
[Required, Display(Name = "Message", Prompt = "Write anything")] | ||
public string? Message { get; set; } | ||
|
||
public async Task OnPost() | ||
{ | ||
// Pretend the server is doing something heavy | ||
await Task.Delay(500); | ||
|
||
Response.Htmx(headers => | ||
{ | ||
headers.WithTrigger("showMessage", new { message = Message }); | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters