You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
v9 Update: First MVC Series - 10 tutorials + app samples (#33192)
* v9 Update First MVC Series - 10 tutorials
* Updated some screenshot links
* Fix link
* Fix code highlite and image link
* Update aspnetcore/tutorials/first-mvc-app/start-mvc.md
Co-authored-by: Tom Dykstra <tdykstra@microsoft.com>
* Review fixes: remove finsihed app screenshots from start-mvc
---------
Co-authored-by: Tom Dykstra <tdykstra@microsoft.com>
The DataAnnotations namespace provides a set of built-in validation attributes that are applied declaratively to a class or property. DataAnnotations also contains formatting attributes like `DataType` that help with formatting and don't provide any validation.
6
+
7
+
Update the `Movie` class to take advantage of the built-in validation attributes `Required`, `StringLength`, `RegularExpression`, `Range` and the `DataType` formatting attribute.
The validation attributes specify behavior that you want to enforce on the model properties they're applied to:
12
+
13
+
* The `Required` and `MinimumLength` attributes indicate that a property must have a value; but nothing prevents a user from entering white space to satisfy this validation.
14
+
* The `RegularExpression` attribute is used to limit what characters can be input. In the preceding code, "Genre":
15
+
16
+
* Must only use letters.
17
+
* The first letter is required to be uppercase. White spaces are allowed while numbers, and special
18
+
characters are not allowed.
19
+
20
+
* The `RegularExpression` "Rating":
21
+
22
+
* Requires that the first character be an uppercase letter.
23
+
* Allows special characters and numbers in subsequent spaces. "PG-13" is valid for a rating, but fails for a "Genre".
24
+
25
+
* The `Range` attribute constrains a value to within a specified range.
26
+
* The `StringLength` attribute lets you set the maximum length of a string property, and optionally its minimum length.
27
+
* Value types (such as `decimal`, `int`, `float`, `DateTime`) are inherently required and don't need the `[Required]` attribute.
28
+
29
+
Having validation rules automatically enforced by ASP.NET Core helps make your app more robust. It also ensures that you can't forget to validate something and inadvertently let bad data into the database.
The DataAnnotations namespace provides a set of built-in validation attributes that are applied declaratively to a class or property. DataAnnotations also contains formatting attributes like `DataType` that help with formatting and don't provide any validation.
6
+
7
+
Update the `Movie` class to take advantage of the built-in validation attributes `Required`, `StringLength`, `RegularExpression`, `Range` and the `DataType` formatting attribute.
The validation attributes specify behavior that you want to enforce on the model properties they're applied to:
12
+
13
+
* The `Required` and `MinimumLength` attributes indicate that a property must have a value; but nothing prevents a user from entering white space to satisfy this validation.
14
+
* The `RegularExpression` attribute is used to limit what characters can be input. In the preceding code, "Genre":
15
+
16
+
* Must only use letters.
17
+
* The first letter is required to be uppercase. White spaces are allowed while numbers, and special
18
+
characters are not allowed.
19
+
20
+
* The `RegularExpression` "Rating":
21
+
22
+
* Requires that the first character be an uppercase letter.
23
+
* Allows special characters and numbers in subsequent spaces. "PG-13" is valid for a rating, but fails for a "Genre".
24
+
25
+
* The `Range` attribute constrains a value to within a specified range.
26
+
* The `StringLength` attribute lets you set the maximum length of a string property, and optionally its minimum length.
27
+
* Value types (such as `decimal`, `int`, `float`, `DateTime`) are inherently required and don't need the `[Required]` attribute.
28
+
29
+
Having validation rules automatically enforced by ASP.NET Core helps make your app more robust. It also ensures that you can't forget to validate something and inadvertently let bad data into the database.
In **Solution Explorer**, control-click **Controllers** and select **Add > New File**.
65
-
66
-

67
-
68
-
Select **ASP.NET Core** and **Controller Class**.
69
-
70
-
Name the controller **HelloWorldController**.
71
-
72
-

73
-
74
61
---
75
62
76
63
Replace the contents of `Controllers/HelloWorldController.cs` with the following code:
@@ -95,7 +82,7 @@ Run the app without the debugger by pressing <kbd>Ctrl</kbd>+<kbd>F5</kbd> (Wind
95
82
96
83
Append `/HelloWorld` to the path in the address bar. The `Index` method returns a string.
97
84
98
-

85
+

99
86
100
87
MVC invokes controller classes, and the action methods within them, depending on the incoming URL. The default [URL routing logic](xref:mvc/controllers/routing) used by MVC, uses a format like this to determine what code to invoke:
101
88
@@ -115,7 +102,7 @@ Browse to: `https://localhost:{PORT}/HelloWorld/Welcome`. Replace `{PORT}` with
115
102
116
103
The `Welcome` method runs and returns the string `This is the Welcome action method...`. For this URL, the controller is `HelloWorld` and `Welcome` is the action method. You haven't used the `[Parameters]` part of the URL yet.
117
104
118
-

105
+

119
106
120
107
Modify the code to pass some parameter information from the URL to the controller. For example, `/HelloWorld/Welcome?name=Rick&numtimes=4`.
121
108
@@ -133,7 +120,7 @@ Run the app and browse to: `https://localhost:{PORT}/HelloWorld/Welcome?name=Ric
133
120
134
121
Try different values for `name` and `numtimes` in the URL. The MVC [model binding](xref:mvc/models/model-binding) system automatically maps the named parameters from the query string to parameters in the method. See [Model Binding](xref:mvc/models/model-binding) for more information.
135
122
136
-

123
+

0 commit comments