Skip to content

Commit

Permalink
Fix the Required attribute description (dotnet#213)
Browse files Browse the repository at this point in the history
* Fix the Required attribute description

* Fix the Required attribute description

* Fix the Required attribute description
  • Loading branch information
cnolewajka authored and Rick-Anderson committed Aug 25, 2019
1 parent 47db9b8 commit bcee67c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,14 @@ In *Models\Student.cs*, replace the code you added earlier with the following co

### The Required Attribute

The [Required attribute](https://msdn.microsoft.com/library/system.componentmodel.dataannotations.requiredattribute.aspx) makes the name properties required fields. The `Required attribute` is not needed for value types such as DateTime, int, double, and float. Value types cannot be assigned a null value, so they are inherently treated as required fields. You could remove the [Required attribute](https://msdn.microsoft.com/library/system.componentmodel.dataannotations.requiredattribute.aspx) and replace it with a minimum length parameter for the `StringLength` attribute:
The [Required attribute](https://msdn.microsoft.com/library/system.componentmodel.dataannotations.requiredattribute.aspx) makes the name properties required fields. The `Required attribute` is not needed for value types such as DateTime, int, double, and float. Value types cannot be assigned a null value, so they are inherently treated as required fields.

The `Required` attribute must be used with `MinimumLength` for the `MinimumLength` to be enforced.

[!code-csharp[Main](creating-a-more-complex-data-model-for-an-asp-net-mvc-application/samples/sample8.cs?highlight=2)]

`MinimumLength` and `Required` allow whitespace to satisfy the validation. Use the `RegularExpression` attribute for full controll over the string.

### The Display Attribute

The `Display` attribute specifies that the caption for the text boxes should be "First Name", "Last Name", "Full Name", and "Enrollment Date" instead of the property name in each instance (which has no space dividing the words).
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[Display(Name = "Last Name")]
[StringLength(50, MinimumLength=1)]
public string LastName { get; set; }
[Required]
[StringLength(50, MinimumLength=2)]
public string LastName { get; set; }

0 comments on commit bcee67c

Please sign in to comment.