Skip to content

Commit

Permalink
Merge pull request #63 from haacked/haacked/add-select-demo
Browse files Browse the repository at this point in the history
Add Demo of select input and validation summary
  • Loading branch information
haacked authored Jul 30, 2023
2 parents d910bd3 + c6e9ffb commit 8b72a43
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
26 changes: 26 additions & 0 deletions Pages/Demos/SelectInput.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
@page
@model DemoWeb.Pages.Demos.SelectInput
@{
Layout = "Shared/_Layout";
}

<div asp-validation-summary="All">
<span>Please correct the following errors</span>
</div>

<fieldset>
<legend>Required Select input with validation summary.</legend>
<form method="post">
<div class="form-field">
<label asp-for="Animal"></label>
<select asp-for="Animal" asp-items="Model.Animals"></select>
<span asp-validation-for="Animal"></span>
</div>
<div class="form-field">
<label asp-for="AnotherRequiredField"></label>
<input asp-for="AnotherRequiredField" />
<span asp-validation-for="AnotherRequiredField"></span>
</div>
<input type="submit" value="Submit"/>
</form>
</fieldset>
25 changes: 25 additions & 0 deletions Pages/Demos/SelectInput.cshtml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace DemoWeb.Pages.Demos;

public class SelectInput : PageModel
{
[BindProperty]
[Required]
public string? Animal { get; set; }

[BindProperty]
[Required]
public string? AnotherRequiredField { get; set; }

public IReadOnlyList<SelectListItem> Animals => new List<SelectListItem>
{
new("None", ""),
new("Dog", "Dog"),
new("Cat", "Cat"),
new("Fish", "Fish"),
};
}
1 change: 1 addition & 0 deletions Pages/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<li><a asp-page="Demos/Checkboxes">Checkboxes and Radio Buttons</a></li>
<li><a asp-page="Demos/SubmitButton">Submit Button</a></li>
<li><a asp-page="Demos/RemovedInputs">Removed Inputs</a></li>
<li><a asp-page="Demos/SelectInput">Select Input and Validation Summary</a></li>
</ul>

@if (Model.StatusMessage != null) {
Expand Down

0 comments on commit 8b72a43

Please sign in to comment.