Skip to content

Commit e11df90

Browse files
Merge pull request #48585 from lootle1/MR68
Technical Review 1016180: Improve how forms and validation work in Bl…
2 parents 27b7128 + fd70af5 commit e11df90

13 files changed

+45
-45
lines changed

learn-pr/aspnetcore/blazor-improve-how-forms-work/1-introduction.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ metadata:
55
unitType: introduction
66
title: Introduction
77
description: Introduction
8-
ms.date: 11/09/2023
8+
ms.date: 01/09/2025
99
author: csharpfritz
1010
ms.author: jefritz
1111
ms.manager: markl

learn-pr/aspnetcore/blazor-improve-how-forms-work/2-attach-csharp-code-dom-events-blazor-event-handlers.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ metadata:
55
unitType: learning-content
66
title: "Attach C# code to DOM events with Blazor event handlers"
77
description: "Create handlers to process DOM events using C#"
8-
ms.date: 11/09/2023
8+
ms.date: 01/09/2025
99
author: csharpfritz
1010
ms.author: jefritz
1111
ms.manager: markl
@@ -20,7 +20,7 @@ quiz:
2020
choices:
2121
- content: "C# events"
2222
isCorrect: false
23-
explanation: "C# events do not properly transfer between components"
23+
explanation: "C# events don't properly transfer between components"
2424
- content: "EventCallback"
2525
isCorrect: true
2626
explanation: "EventCallbacks look like parameters on a component and can be specified on a Blazor page or another component"
@@ -34,7 +34,7 @@ quiz:
3434
explanation: "preventDefault prevents the default action in the DOM element from executing"
3535
- content: "return false"
3636
isCorrect: false
37-
explanation: "return false was a technique to stop an event in other JavaScript frameworks, but does not prevent the default action"
37+
explanation: "return false was a technique to stop an event in other JavaScript frameworks, but doesn't prevent the default action"
3838
- content: "throw new Exception()"
3939
isCorrect: false
4040
explanation: "Throwing an exception might stop the default action from executing, but it also raises an error that you might not want your application users to interact with"

learn-pr/aspnetcore/blazor-improve-how-forms-work/3-exercise-create-blazor-event-handler-onclick-events.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ metadata:
55
unitType: Exercise
66
title: "Exercise - Create a Blazor event handler for onclick events"
77
description: "Handle the onclick event for an HTML form control using C#"
8-
ms.date: 11/09/2023
8+
ms.date: 01/09/2025
99
author: csharpfritz
1010
ms.author: jefritz
1111
ms.manager: markl

learn-pr/aspnetcore/blazor-improve-how-forms-work/4-take-advantage-power-blazor-forms.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ metadata:
55
unitType: learning-content
66
title: "Take advantage of the power of Blazor forms"
77
description: "Learn how to use Blazor input elements with Blazor forms"
8-
ms.date: 11/09/2023
8+
ms.date: 01/09/2025
99
author: csharpfritz
1010
ms.author: jefritz
1111
ms.manager: markl
@@ -23,18 +23,18 @@ quiz:
2323
explanation: "This event executes when EditForm fields are submitted with invalid content"
2424
- content: "OnFailedValidation"
2525
isCorrect: false
26-
explanation: "This sounds like a good name for an event, but is not correct"
26+
explanation: "This sounds like a good name for an event, but isn't correct"
2727
- content: "OnFailedSubmit"
2828
isCorrect: false
29-
explanation: "'Failed' is not part of the event name that is raised"
29+
explanation: "'Failed' isn't part of the event name that is raised"
3030
- content: "An EditForm has the following features EXCEPT:"
3131
choices:
3232
- content: "Data Binding"
3333
isCorrect: false
3434
explanation: "The EditForm can be associated with an object and then acts like a view of the object for data entry purposes"
3535
- content: "Data cleansing"
3636
isCorrect: true
37-
explanation: "While you can edit the content of an EditForm, it does not perform data cleansing operations on its own"
37+
explanation: "While you can edit the content of an EditForm, it doesn't perform data cleansing operations on its own"
3838
- content: "Validation"
3939
isCorrect: false
4040
explanation: "An EditForm extensive and extensible validation capabilities"

learn-pr/aspnetcore/blazor-improve-how-forms-work/5-exercise-create-address-form-blazor-components.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ metadata:
55
unitType: Exercise
66
title: "Exercise - Create an address form with Blazor components"
77
description: "Build an address form and connect it to a data model."
8-
ms.date: 11/09/2023
8+
ms.date: 01/09/2025
99
author: csharpfritz
1010
ms.author: jefritz
1111
ms.manager: markl

learn-pr/aspnetcore/blazor-improve-how-forms-work/6-validate-user-input-implicitly.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ metadata:
55
unitType: learning-content
66
title: "Validate user input implicitly without writing validation code"
77
description: "Learn how to use Blazor's events to validate user input in forms."
8-
ms.date: 11/09/2023
8+
ms.date: 01/09/2025
99
author: csharpfritz
1010
ms.author: jefritz
1111
ms.manager: markl
@@ -20,18 +20,18 @@ quiz:
2020
choices:
2121
- content: "ValidationMessage"
2222
isCorrect: false
23-
explanation: "The ValidationMessage component will display the message for one component, not a summary"
23+
explanation: "The ValidationMessage component displays the message for one component, not a summary"
2424
- content: "DataAnnotationsValidator"
2525
isCorrect: false
26-
explanation: "This component activates the validations in the EditForm, but does not display a summary of validation message"
26+
explanation: "This component activates the validations in the EditForm, but doesn't display a summary of validation message"
2727
- content: "ValidationSummary"
2828
isCorrect: true
2929
explanation: "This component displays a summary of all the validation messages in a submitted form"
30-
- content: "Which of these is NOT a standard input elements that comes with Blazor?"
30+
- content: "Which of these choices is NOT a standard input element that comes with Blazor?"
3131
choices:
3232
- content: "Guid"
3333
isCorrect: true
34-
explanation: "Guid is not part of the standard input elements provided by Blazor"
34+
explanation: "Guid isn't part of the standard input elements provided by Blazor"
3535
- content: "CreditCard"
3636
isCorrect: false
3737
explanation: "CreditCard is one of the standard input elements provided by Blazor"

learn-pr/aspnetcore/blazor-improve-how-forms-work/7-exercise-add-server-client-side-data-validation-address-form.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ metadata:
55
unitType: Exercise
66
title: "Exercise - Add server-side and client-side data validation to the address form "
77
description: "Use data annotations on a model to add custom validation to a Blazor EditForm component."
8-
ms.date: 11/09/2023
8+
ms.date: 01/09/2025
99
author: csharpfritz
1010
ms.author: jefritz
1111
ms.manager: markl

learn-pr/aspnetcore/blazor-improve-how-forms-work/8-summary.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ metadata:
55
unitType: summary
66
title: "Summary"
77
description: "In this unit, you'll review what you've learned about forms in Blazor web apps."
8-
ms.date: 11/09/2023
8+
ms.date: 01/09/2025
99
author: csharpfritz
1010
ms.author: jefritz
1111
ms.manager: markl

learn-pr/aspnetcore/blazor-improve-how-forms-work/includes/2-attach-csharp-code-dom-events-blazor-event-handlers.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ In this unit, you'll look in detail at the third option; how to create a Blazor
88

99
## Handle an event with Blazor and C#
1010

11-
Each element in the HTML markup of a Blazor app supports many events. Most of these events correspond to the DOM events available in regular web applications, but you can also create user-defined events that are triggered by writing code. To capture an event with Blazor, you write a C# method that handles the event, then bind the event to the method with a Blazor directive. For a DOM event, the Blazor directive shares the same name as the equivalent HTML event, such as `@onkeydown` or `@onfocus`. For example, the sample app generated by using the Blazor Server App contains the following code on the **Counter.razor** page. This page displays a button. When the user selects the button, the `@onclick` event triggers the `IncrementCount` method that increments a counter indicating how many times the button has been clicked. The value of the counter variable is displayed by the \<p\> element on the page:
11+
Each element in the HTML markup of a Blazor app supports many events. Most of these events correspond to the DOM events available in regular web applications, but you can also create user-defined events that are triggered by writing code. To capture an event with Blazor, you write a C# method that handles the event, then bind the event to the method with a Blazor directive. For a DOM event, the Blazor directive shares the same name as the equivalent HTML event, such as `@onkeydown` or `@onfocus`. For example, the sample app generated by using the Blazor Server App contains the following code on the **Counter.razor** page. This page displays a button. When the user selects the button, the `@onclick` event triggers the `IncrementCount` method that increments a counter indicating how many times the button has been clicked. The \<p\> element on the page displays the value of the counter variable:
1212

1313
```razor
1414
@page "/counter"
@@ -84,7 +84,7 @@ A traditional web application uses JavaScript to capture and process events. You
8484

8585
Besides the syntactic differences in the two versions of the event handler, you should note the following functional differences:
8686

87-
- JavaScript does not prefix the name of the event with an `@` sign; it's not a Blazor directive.
87+
- JavaScript doesn't prefix the name of the event with an `@` sign; it's not a Blazor directive.
8888
- In the Blazor code, you specify the name of the event-handling method when you attach it to an event. In JavaScript, you write a statement that calls the event-handling method; you specify round brackets and any parameters required.
8989
- Most importantly, the JavaScript event handler runs in the browser, on the client. If you're building a Blazor Server App, the Blazor event handler runs on the server and only updates the browser with any changes made to the UI when the event handler completes. Additionally, the Blazor mechanism enables an event handler to access static data that's shared between sessions; the JavaScript model doesn't. However, handling some frequently occurring events such as `@onmousemove` can cause the user interface to become sluggish because they require a network round-trip to the server. You might prefer to handle events such as these in the browser, using JavaScript.
9090

@@ -93,7 +93,7 @@ Besides the syntactic differences in the two versions of the event handler, you
9393
9494
## Handle events asynchronously
9595

96-
By default, Blazor event handlers are synchronous. If an event handler performs a potentially long-running operation, such as calling a web service, the thread on which the event handler runs will be blocked until the operation completes. This can lead to poor response in the user interface. To combat this, you can designate an event handler method as asynchronous. Use the C# `async` keyword. The method must return a `Task` object. You can then use the `await` operator inside the event handler method to initiate any long-running tasks on a separate thread and free the current thread for other work. When a long-running task completes, the event handler resumes. The example event handler below runs a time-consuming method asynchronously:
96+
By default, Blazor event handlers are synchronous. If an event handler performs a potentially long-running operation, such as calling a web service, the thread on which the event handler runs will be blocked until the operation completes. This can lead to poor response in the user interface. To combat this, you can designate an event handler method as asynchronous. Use the C# `async` keyword. The method must return a `Task` object. You can then use the `await` operator inside the event handler method to initiate any long-running tasks on a separate thread and free the current thread for other work. When a long-running task completes, the event handler resumes. The example event handler that follows runs a time-consuming method asynchronously:
9797

9898
```razor
9999
<button @onclick="DoWork">Run time-consuming operation</button>
@@ -144,7 +144,7 @@ The following image shows the result when the user selects the button:
144144
:::image type="content" source="../media/2-change-focus.png" alt-text="Screenshot of the web page after the user has clicked the button to set the focus to the input element.":::
145145

146146
> [!NOTE]
147-
> An app should only direct the focus to a specific control for a specific reason, such as to ask the user to modify input after an error. Don't use focusing to force the user to navigate through the elements on a page in a fixed order; this can be very frustrating to the user who might want to revisit elements to change their input.
147+
> An app should only direct the focus to a specific control for a specific reason, such as to ask the user to modify input after an error. Don't use focusing to force the user to navigate through the elements on a page in a fixed order. This can be frustrating to the user who might want to revisit elements to change their input.
148148
149149
## Write inline event handlers
150150

@@ -167,7 +167,7 @@ C# supports lambda expressions. A lambda expression enables you to create an ano
167167
> [!NOTE]
168168
> For details on how lambda expressions work, read [Lambda expressions and anonymous functions](/dotnet/csharp/language-reference/operators/lambda-expressions).
169169
170-
This approach is also useful if you want to provide other arguments for an event-handling method. In the following example, the method `HandleClick` takes a `MouseEventArgs` parameter in the same way as an ordinary click event handler, but it also accepts a string parameter. The method processes the click event as before, but also displays the message in the user has pressed the <kbd>Ctrl</kbd> key. The lambda expression calls the `HandleCLick` method, passing in the `MouseEventArgs` parameter (`mouseEvent`), and a string.
170+
This approach is also useful if you want to provide other arguments for an event-handling method. In the following example, the method `HandleClick` takes a `MouseEventArgs` parameter in the same way as an ordinary click event handler, but it also accepts a string parameter. The method processes the click event as before, but also displays the message if the user has pressed the <kbd>Ctrl</kbd> key. The lambda expression calls the `HandleCLick` method, passing in the `MouseEventArgs` parameter (`mouseEvent`), and a string.
171171

172172
```razor
173173
@page "/counter"
@@ -202,7 +202,7 @@ This approach is also useful if you want to provide other arguments for an event
202202
203203
## Override default DOM actions for events
204204

205-
Several DOM events have default actions that run when the event occurs, regardless of whether there's an event handler available for that event. For example, the `@onkeypress` event for an \<input\> element always displays the character that corresponds to the key that the user has pressed, and handling the key press. In the next example, the `@onkeypress` event is used to convert the user's input to uppercase. Additionally, if the user types an `@` character, the event handler displays an alert:
205+
Several DOM events have default actions that run when the event occurs, regardless of whether there's an event handler available for that event. For example, the `@onkeypress` event for an \<input\> element always displays the character that corresponds to the key that the user has pressed and handles the key press. In the next example, the `@onkeypress` event is used to convert the user's input to uppercase. Additionally, if the user types an `@` character, the event handler displays an alert:
206206

207207
```razor
208208
<input value=@data @onkeypress="ProcessKeyPress"/>
@@ -279,7 +279,7 @@ A Blazor page can contain one or more Blazor components, and components can be n
279279

280280
A callback can take a single parameter. `EventCallback` is a generic type. The type parameter specifies the type of the argument passed to the callback.
281281

282-
As an example, consider the following scenario. You want to create a component named `TextDisplay` that enables the user to enter an input string and transform that string in some way; you might want to convert it to upper case, lower case, mixed case, filter characters from it, or perform some other type of transformation. However, when you write the code for the `TextDisplay` component, you don't know what the transformation process will be, and instead want to defer this operation to another component. The following code shows the `TextDisplay` component. It provides the input string in the form of an \<input\> element that enables the user to enter a text value.
282+
As an example, consider the following scenario. You want to create a component named `TextDisplay` that enables the user to enter an input string and transform that string in some way. You might want to convert it to upper case, lower case, mixed case, filter characters from it, or perform some other type of transformation. However, when you write the code for the `TextDisplay` component, you don't know what the transformation process will be. Instead, you want to defer this operation to another component. The following code shows the `TextDisplay` component. It provides the input string in the form of an \<input\> element that enables the user to enter a text value.
283283

284284
```razor
285285
@* TextDisplay component *@
@@ -318,7 +318,7 @@ namespace WebApplication.Data
318318

319319
The `key` field contains the value entered by the user, and the `TransformedKey` field will hold the transformed value of the key when it has been processed.
320320

321-
In this example, the `EventCallback` object is a component parameter, and its value is supplied when the component is created. This action is performed by another component, named `TextTransformer`:
321+
In this example, the `EventCallback` object is a component parameter, and its value is supplied when the component is created. The component named `TextTransformer` performs this action:
322322

323323
```razor
324324
@page "/texttransformer"

0 commit comments

Comments
 (0)