Book.cs
using System.ComponentModel.DataAnnotations;
[ Key ]
DataAnnotations
namespace is used to designate a property as the primary key of a model class.
[ Required ]
[Required]
attribute from the DataAnnotations
namespace is crucial for marking properties as mandatory.
get;
set;
[ Display( Name = "Book Title" ) ]
[Display]
attribute is commonly used in models to specify metadata for properties, such as display names, descriptions, or formatting.
This is particularly helpful when working with forms or views, as it allows you to customize how data is presented to users.
[ DataType( DataType.Currency ) ]
[DataType]
attribute indicates that the property represents a currency value.
[ Range( 1, 100 ) ]
[Range]
attribute is used to specify the minimum and maximum values for a property in a model.
|
using System.ComponentModel.DataAnnotations; namespace BookStore1.Models { public class Book { [ Key ] [ Required ] public int Id { get; set; } [ Display( Name = "Book Title" ) ] [ Required ] public string Title { get; set; } public string Genre { get; set; } [ DataType( DataType.Currency ) ] [ Range( 1, 100 ) ] public decimal Price { get; set; } [ Display( Name = "Publish Date" ) ] [ DataType( DataType.Date ) ] public DateTime PublishDate { get; set; } } } |
“I love being married. It’s great to find that one special person you want to annoy for the rest of your life.” — Rita Rudner |