Code simplification. (#5171)
* Code simplification. Deleting a lot of empty lines and simplification of a regular expressions. * Update adding-validation.mdpull/5167/head
parent
da90a2ded7
commit
d5642ae42e
|
@ -37,7 +37,7 @@ Open the *Movie.cs* file. Notice the [`System.ComponentModel.DataAnnotations`](h
|
|||
|
||||
Now update the `Movie` class to take advantage of the built-in [`Required`](https://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.requiredattribute.aspx), [`StringLength`](https://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.stringlengthattribute.aspx), [RegularExpression](https://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.regularexpressionattribute.aspx), and [`Range`](https://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.rangeattribute.aspx) validation attributes. Replace the `Movie` class with the following:
|
||||
|
||||
[!code-csharp[Main](adding-validation/samples/sample1.cs?highlight=8,22-24,30-31,37-38)]
|
||||
[!code-csharp[Main](adding-validation/samples/sample1.cs?highlight=5,13-15,18-19,22-23)]
|
||||
|
||||
The [`StringLength`](https://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.stringlengthattribute.aspx) attribute sets the maximum length of the string, and it sets this limitation on the database, therefore the database schema will change. Right click on the **Movies** table in **Server explorer** and click **Open Table Definition**:
|
||||
|
||||
|
|
|
@ -2,39 +2,24 @@ public class Movie
|
|||
{
|
||||
public int ID { get; set; }
|
||||
|
||||
|
||||
|
||||
|
||||
[StringLength(60, MinimumLength = 3)]
|
||||
public string Title { get; set; }
|
||||
|
||||
|
||||
|
||||
|
||||
[Display(Name = "Release Date")]
|
||||
[DataType(DataType.Date)]
|
||||
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
|
||||
public DateTime ReleaseDate { get; set; }
|
||||
|
||||
|
||||
|
||||
|
||||
[RegularExpression(@"^[A-Z]+[a-zA-Z''-'\s]*$")]
|
||||
[RegularExpression(@"^[A-Z]+[a-zA-Z'\s]*$")]
|
||||
[Required]
|
||||
[StringLength(30)]
|
||||
public string Genre { get; set; }
|
||||
|
||||
|
||||
|
||||
|
||||
[Range(1, 100)]
|
||||
[DataType(DataType.Currency)]
|
||||
public decimal Price { get; set; }
|
||||
|
||||
|
||||
|
||||
|
||||
[RegularExpression(@"^[A-Z]+[a-zA-Z''-'\s]*$")]
|
||||
[RegularExpression(@"^[A-Z]+[a-zA-Z'\s]*$")]
|
||||
[StringLength(5)]
|
||||
public string Rating { get; set; }
|
||||
}
|
Loading…
Reference in New Issue