diff --git a/Controllers/Validations.cs b/Controllers/Validations.cs index 28d793e..fbeead1 100644 --- a/Controllers/Validations.cs +++ b/Controllers/Validations.cs @@ -11,9 +11,9 @@ public IActionResult CheckRemote(string id) } [HttpPost] - public IActionResult CheckboxRemote(bool isChecked) + public IActionResult CheckboxRemote(bool isChecked, bool isCheckedToo) { - return Ok(isChecked); + return Ok(isChecked == isCheckedToo); } [HttpGet] diff --git a/Pages/Demos/Checkboxes.cshtml b/Pages/Demos/Checkboxes.cshtml index e366c99..c1a6df1 100644 --- a/Pages/Demos/Checkboxes.cshtml +++ b/Pages/Demos/Checkboxes.cshtml @@ -15,6 +15,7 @@ Required ASP.NET Checkboxes (with hidden input) and Radio buttons
+

ASP.NET renders a checkbox and a hidden input for each boolean property. @@ -32,6 +33,20 @@ }

+
+

+ One can also use the [Remote] validation attribute with checkboxes. + This checkbox's remote validator returns valid only when it matches the one above. + Note: Changes to + additional fields + do not automatically revalidate. +

+ + +
+

However, if you manually render a checkbox, the checkbox is only submitted @@ -101,6 +116,7 @@

+
diff --git a/Pages/Demos/Checkboxes.cshtml.cs b/Pages/Demos/Checkboxes.cshtml.cs index 7501d91..21b2b3a 100644 --- a/Pages/Demos/Checkboxes.cshtml.cs +++ b/Pages/Demos/Checkboxes.cshtml.cs @@ -41,8 +41,13 @@ public IActionResult OnPost() public class InputModel { - [Remote("CheckboxRemote", "Validations", HttpMethod = "Post")] public bool IsChecked { get; set; } + + [Remote("CheckboxRemote", "Validations", HttpMethod = "Post", + ErrorMessage = "Must match other checkbox.", + AdditionalFields = $"{nameof(IsChecked)}" + )] + public bool IsCheckedToo { get; set; } } [BindProperty] @@ -54,4 +59,4 @@ public class Selectable public bool IsSelected { get; set; } } -} \ No newline at end of file +}