From e596cce731a5a38a0d516d7c295d0c40b602f495 Mon Sep 17 00:00:00 2001 From: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> Date: Tue, 31 Aug 2021 15:04:54 -0700 Subject: [PATCH] fix validation snippets /6/ra (#23183) * fix validation snippets /6/ra * fix validation snippets /6/ra --- .../RazorPagesMovie30/Models/MovieDateRatingDA.cs | 2 +- .../RazorPagesMovie60/Models/MovieDateRatingDA.cs | 4 ++-- .../RazorPagesMovie60/Models/MovieDateRatingDAmult.cs | 10 +++++----- aspnetcore/tutorials/razor-pages/validation.md | 10 +++++----- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/aspnetcore/tutorials/razor-pages/razor-pages-start/sample/RazorPagesMovie30/Models/MovieDateRatingDA.cs b/aspnetcore/tutorials/razor-pages/razor-pages-start/sample/RazorPagesMovie30/Models/MovieDateRatingDA.cs index 29207b28eb..16ac845c48 100644 --- a/aspnetcore/tutorials/razor-pages/razor-pages-start/sample/RazorPagesMovie30/Models/MovieDateRatingDA.cs +++ b/aspnetcore/tutorials/razor-pages/razor-pages-start/sample/RazorPagesMovie30/Models/MovieDateRatingDA.cs @@ -38,6 +38,6 @@ namespace RazorPagesMovie.Models [Required] public string Rating { get; set; } } - #endregion } +#endregion #endif diff --git a/aspnetcore/tutorials/razor-pages/razor-pages-start/sample/RazorPagesMovie60/Models/MovieDateRatingDA.cs b/aspnetcore/tutorials/razor-pages/razor-pages-start/sample/RazorPagesMovie60/Models/MovieDateRatingDA.cs index 1b894a5c62..e5643cb2e5 100644 --- a/aspnetcore/tutorials/razor-pages/razor-pages-start/sample/RazorPagesMovie60/Models/MovieDateRatingDA.cs +++ b/aspnetcore/tutorials/razor-pages/razor-pages-start/sample/RazorPagesMovie60/Models/MovieDateRatingDA.cs @@ -1,4 +1,4 @@ -#define MovieDateRatingDA +//#define MovieDateRatingDA #if MovieDateRatingDA #region snippet1 using System.ComponentModel.DataAnnotations; @@ -37,6 +37,6 @@ namespace RazorPagesMovie.Models [Required] public string Rating { get; set; } = string.Empty; } - #endregion } +#endregion #endif diff --git a/aspnetcore/tutorials/razor-pages/razor-pages-start/sample/RazorPagesMovie60/Models/MovieDateRatingDAmult.cs b/aspnetcore/tutorials/razor-pages/razor-pages-start/sample/RazorPagesMovie60/Models/MovieDateRatingDAmult.cs index ea9653f28b..3582e75dcf 100644 --- a/aspnetcore/tutorials/razor-pages/razor-pages-start/sample/RazorPagesMovie60/Models/MovieDateRatingDAmult.cs +++ b/aspnetcore/tutorials/razor-pages/razor-pages-start/sample/RazorPagesMovie60/Models/MovieDateRatingDAmult.cs @@ -1,4 +1,4 @@ -//#define COMBINED +#define COMBINED #if COMBINED #region snippet1 using System; @@ -12,21 +12,21 @@ namespace RazorPagesMovie.Models public int ID { get; set; } [StringLength(60, MinimumLength = 3)] - public string Title { get; set; } + public string Title { get; set; } = string.Empty; [Display(Name = "Release Date"), DataType(DataType.Date)] public DateTime ReleaseDate { get; set; } [RegularExpression(@"^[A-Z]+[a-zA-Z\s]*$"), Required, StringLength(30)] - public string Genre { get; set; } + public string Genre { get; set; } = string.Empty; [Range(1, 100), DataType(DataType.Currency)] [Column(TypeName = "decimal(18, 2)")] public decimal Price { get; set; } [RegularExpression(@"^[A-Z]+[a-zA-Z0-9""'\s-]*$"), StringLength(5)] - public string Rating { get; set; } + public string Rating { get; set; } = string.Empty; } -#endregion } +#endregion #endif diff --git a/aspnetcore/tutorials/razor-pages/validation.md b/aspnetcore/tutorials/razor-pages/validation.md index a1db776d64..89a9f6029e 100644 --- a/aspnetcore/tutorials/razor-pages/validation.md +++ b/aspnetcore/tutorials/razor-pages/validation.md @@ -37,7 +37,7 @@ The `DataAnnotations` namespace provides: Update the `Movie` class to take advantage of the built-in `[Required]`, `[StringLength]`, `[RegularExpression]`, and `[Range]` validation attributes. -[!code-csharp[](~/tutorials/razor-pages/razor-pages-start/sample/RazorPagesMovie30/Models/MovieDateRatingDA.cs?name=snippet1)] +[!code-csharp[](~/tutorials/razor-pages/razor-pages-start/sample/RazorPagesMovie60/Models/MovieDateRatingDA.cs?name=snippet1)] The validation attributes specify behavior to enforce on the model properties they're applied to: @@ -121,7 +121,7 @@ When validation logic needs to change, it's done only in the model. Validation i Examine the `Movie` class. The `System.ComponentModel.DataAnnotations` namespace provides formatting attributes in addition to the built-in set of validation attributes. The `[DataType]` attribute is applied to the `ReleaseDate` and `Price` properties. -[!code-csharp[](razor-pages-start/sample/RazorPagesMovie/Models/MovieDateRatingDA.cs?highlight=2,6&name=snippet2)] +[!code-csharp[](razor-pages-start/sample/RazorPagesMovie60/Models/MovieDateRatingDA.cs?highlight=2,6&name=snippet2)] The `[DataType]` attributes provide: @@ -168,7 +168,7 @@ It's a best practice to avoid compiling hard dates in models, so using the `[Ran The following code shows combining attributes on one line: -[!code-csharp[](razor-pages-start/sample/RazorPagesMovie30/Models/MovieDateRatingDAmult.cs?name=snippet1)] +[!code-csharp[](razor-pages-start/sample/RazorPagesMovie60/Models/MovieDateRatingDAmult.cs?name=snippet1)] [Get started with Razor Pages and EF Core](xref:data/ef-rp/intro) shows advanced EF Core operations with Razor Pages. @@ -176,7 +176,7 @@ The following code shows combining attributes on one line: The DataAnnotations applied to the class changes the schema. For example, the DataAnnotations applied to the `Title` field: -[!code-csharp[](razor-pages-start/sample/RazorPagesMovie30/Models/MovieDateRatingDA.cs?name=snippet11)] +[!code-csharp[](razor-pages-start/sample/RazorPagesMovie60/Models/MovieDateRatingDA.cs?name=snippet11)] * Limits the characters to 60. * Doesn't allow a `null` value. @@ -266,7 +266,7 @@ The `DataAnnotations` namespace provides: Update the `Movie` class to take advantage of the built-in `[Required]`, `[StringLength]`, `[RegularExpression]`, and `[Range]` validation attributes. -[!code-csharp[](~/tutorials/razor-pages/razor-pages-start/sample/RazorPagesMovie60/Models/MovieDateRatingDA.cs?name=snippet1)] +[!code-csharp[](~/tutorials/razor-pages/razor-pages-start/sample/RazorPagesMovie30/Models/MovieDateRatingDA.cs?name=snippet1)] The validation attributes specify behavior to enforce on the model properties they're applied to: