Skip to content

Commit a2ac3a7

Browse files
committed
Add sample for form handling
1 parent ce1edee commit a2ac3a7

File tree

11 files changed

+174
-3
lines changed

11 files changed

+174
-3
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# Samples for ASP.NET Core 6.0, 7.0 and 8.0 Preview 7 (491)
1+
# Samples for ASP.NET Core 6.0, 7.0 and 8.0 Preview 7 (492)
22

3-
- Samples for ASP.NET Core **8.0 Preview 7** is available [here](/projects/.net8) (29).
3+
- Samples for ASP.NET Core **8.0 Preview 7** is available [here](/projects/.net8) (30).
44
- Samples for ASP.NET Core **7.0** is available [here](/projects/.net7) (45).
55
- Samples for ASP.NET Core **8.0 Preview 6** using EdgeDB.NET is [here](https://github.com/edgedb/edgedb-net).
66

projects/.net8/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# ASP.NET 8.0 Preview 7 (29)
1+
# ASP.NET 8.0 Preview 7 (30)
22

33
These samples require [.NET 8.0 Preview 7](https://github.com/dotnet/installer#table).
44

@@ -88,6 +88,10 @@ These samples require [.NET 8.0 Preview 7](https://github.com/dotnet/installer#t
8888

8989
This example shows how to perform **multiple** automatic data binding for a form `POST` request using `<EditForm/>` and `[SupplyParameterFromForm]`.
9090

91+
* [RazorFormHandlingFive](RazorFormHandlingFive)
92+
93+
This example shows how to perform data validation using `DataAnnotationsValidator` and `EditForm`.
94+
9195
## Mix and Match
9296

9397
* [RazorMixMatchOne](RazorMixMatchOne)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"dotnet.defaultSolution": "RazorFormHandlingOne.sln"
3+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<html>
2+
<head>
3+
<link href="https://fastly.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-9ndCyUaIbzAi2FUVXJi0CjmCapSmO7SnpJef0486qhLnuZ2cdeRhO02iuK6FUUVM" crossorigin="anonymous">
4+
</head>
5+
<body>
6+
<Router AppAssembly="typeof(Program).Assembly">
7+
<Found Context="routeData">
8+
<RouteView RouteData="routeData" />
9+
</Found>
10+
<NotFound>
11+
<h1>Page not found</h1>
12+
<p>Sorry, but there's nothing here!</p>
13+
</NotFound>
14+
</Router>
15+
</body>
16+
</html>
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
@page "/"
2+
@using System.ComponentModel.DataAnnotations;
3+
4+
<h1>Form Validation using DataAnnotationsValidator and EditForm</h1>
5+
6+
<div class="container">
7+
@((MarkupString)_showMessage)
8+
9+
<br />
10+
<div class="row">
11+
<div class="col-md-6">
12+
<EditForm method="POST" Model="PersonInfo" FormName="info" OnValidSubmit="HandleSubmit">
13+
<DataAnnotationsValidator />
14+
<div class="mb-3">
15+
<label class="form-label" for="PersonInfo.Name">Please enter your name</label><br />
16+
<InputText @bind-Value="PersonInfo.Name" class="form-control" />
17+
<ValidationMessage For="() => PersonInfo.Name" class="text-danger" />
18+
</div>
19+
<div class="mb-3">
20+
<label for="PersonInfo.Description" class="form-label">Describe yourself</label>
21+
<InputTextArea @bind-Value="PersonInfo.Description" class="form-control" />
22+
<ValidationMessage For="() => PersonInfo.Description" class="text-danger" />
23+
</div>
24+
<div class="mb-3">
25+
<label for="PersonInfo.Hobby" class="form-label">Hobby</label>
26+
<InputSelect @bind-Value="PersonInfo.Hobby" class="form-select">
27+
<option>Football</option>
28+
<option>Basketball</option>
29+
<option>Tennis</option>
30+
</InputSelect>
31+
</div>
32+
<div class="mb-3 form-check">
33+
<InputCheckbox @bind-Value="PersonInfo.IsMarried" class="form-check-input" />
34+
<label for="PersonInfo.IsMarried" class="form-check-label">Is married?</label>
35+
</div>
36+
<div class="mb-3 form-check">
37+
@{ var idx = 0;}
38+
<InputRadioGroup @bind-Value="PersonInfo.Nationality">
39+
@foreach(var val in new string[] { "USA", "Indonesia", "Egypt"})
40+
{
41+
var id = "nationality" + idx;
42+
<InputRadio class="formcheck-input" Value="@val" id="@id"/>
43+
<label class="form-check-label" for="@id">@val</label>
44+
<br/>
45+
idx++;
46+
}
47+
</InputRadioGroup>
48+
<ValidationMessage For="() => PersonInfo.Nationality" class="text-danger" />
49+
</div>
50+
<div class="mb-3">
51+
<button type="submit" class="btn btn-primary">Submit</button>
52+
</div>
53+
</EditForm>
54+
</div>
55+
</div>
56+
</div>
57+
58+
@code
59+
{
60+
[SupplyParameterFromForm(Handler="info")]// matches @fornname"info"
61+
public Person PersonInfo { get; set; } = new();
62+
63+
string _showMessage = string.Empty;
64+
65+
void HandleSubmit()
66+
{
67+
_showMessage = $"Name: {PersonInfo.Name}<br />Description: {PersonInfo.Description}<br />Hobby: {PersonInfo.Hobby}<br />Is married: {PersonInfo.IsMarried}<br />Nationality: {PersonInfo.Nationality}<br/><br/>";
68+
}
69+
70+
public class Person
71+
{
72+
[Required]
73+
public string Name { get; set; } = string.Empty;
74+
75+
[Required]
76+
public string Description { get; set; } = string.Empty;
77+
78+
public string Hobby { get; set; } = string.Empty;
79+
80+
public bool IsMarried { get; set; }
81+
82+
[Required]
83+
public string Nationality { get; set; } = string.Empty;
84+
}
85+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
var builder = WebApplication.CreateBuilder();
2+
builder.Services.AddRazorComponents();
3+
4+
var app = builder.Build();
5+
6+
app.MapRazorComponents<RazorFormHandlingOne.Pages.App>();
7+
app.Run();
8+
9+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Validation on Blazor SSR Form using DataAnnotationsValidator and EditForm
2+
3+
This example shows how to perform data validation using `DataAnnotationsValidator` and `EditForm`.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
<PropertyGroup>
3+
<TargetFramework>net8.0</TargetFramework>
4+
<ImplicitUsings>true</ImplicitUsings>
5+
</PropertyGroup>
6+
</Project>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.5.002.0
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RazorFormHandlingOne", "RazorFormHandlingOne.csproj", "{89A0E326-6533-485A-912B-A7C01569C766}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{89A0E326-6533-485A-912B-A7C01569C766}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{89A0E326-6533-485A-912B-A7C01569C766}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{89A0E326-6533-485A-912B-A7C01569C766}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{89A0E326-6533-485A-912B-A7C01569C766}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {6CB93F54-D0BC-42E9-A28F-3947EB913747}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
@inherits LayoutComponentBase
2+
<!doctype html>
3+
<html lang="en">
4+
<head>
5+
<meta charset="utf-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1">
7+
<title>Blazor United</title>
8+
<link href="https://fastly.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-aFq/bzH65dt+w6FI2ooMVUpc+21e0SRygnTpmBvdBgSdnuTN7QbdgL+OapgHtvPp" crossorigin="anonymous">
9+
</head>
10+
<body>
11+
@Body
12+
</body>
13+
</html>

0 commit comments

Comments
 (0)