The asp-for attribute in an <input> element will render HTML and JavaScript that handle the display and data entry for a field based on the model annotations. The JavaScript will set the valid flag on the field.
The asp-validation-for attribute in a <span> element will display any error message generated when the property annotations are not valid.
In this example, the <span> be rendered as this HTML:
<span class="field-validation-valid" data-valmsg-for="Continent.Name" data-valmsg-replace="true"></span>
<div><label asp-for="Continent.Name"></label><div><input asp-for="Continent.Name" /><span asp-validation-for="Continent.Name"></span></div></div>
The [Display] attribute specifies the caption for a label, textbox, or table heading.
Within a Razor Page, the @Html.DisplayForName() helper defaults to the property name unless the [Display] attribute overrides it. In this case, Continent is displayed instead of the more technical ContinentID.
using System.ComponentModel.DataAnnotations;public class Country{[Display(Name = "Continent")]public string ContinentID { get; set; }}
The [DisplayFormat] attribute can explicitly apply a C# format string. The optional ApplyFormatInEditMode means the format should also apply in edit mode.
[DisplayFormat] is often used in combination with the [DataType] attribute. Together they determine the rendered HTML when using the @Html.DisplayFor() helper.
using System.ComponentModel.DataAnnotations;public class Country{[DisplayFormat(DataFormatString = "{0:N0}", ApplyFormatInEditMode = true)]public int? Population { get; set; }}
The [DataType] attribute specifies a more specific data type than the database column type. In this case, the database table will use a DateTime column but the render logic will only show the date.
The @Html.DisplayFor() helper knows about types and will render a default format to match the type. In this case, the HTML5 browser date picker will appear when editing the field.
using System.ComponentModel.DataAnnotations;public class Country{[DataType(DataType.Date)][DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]public DateTime? UnitedNationsDate { get; set; }}
The [Required] attribute can be applied to one or more properties in a database model class. EF will create a NOT NULL column in the database table for the property.
The client-side JavaScript validation scripts will ensure that a non-empty string or number is valid before posting the record from the browser on inserts and updates.
using System.ComponentModel.DataAnnotations;public class Country{[Required]public string Name { get; set; }}
The [RegularExpression] attribute can apply detailed restrictions for data input. The match expression is evaluated during data entry and the result returns true or false. If false, the model state will not be valid and the optional ErrorMessage will display.
In a Razor page, the @Html.DisplayFor() helper only shows the data in the field. The asp-validation-for attribute on a <span> tag displays the ErrorMessage.
using System.ComponentModel.DataAnnotations;public class Country{[RegularExpression(@"[A-Z]+", ErrorMessage = "Only upper case characters are allowed.")]public string CountryCode { get; set; }}
The [StringLength] attribute specifies the maximum length of characters that are allowed in a data field and optionally the minimum length. The model will not be flagged as valid if these restrictions are exceeded. In this case, the ContinentID must be exactly 2 characters in length.
In a Razor Page, the @Html.DisplayFor() helper only shows the data in the field. The client-side JavaScript validation scripts use the asp-validation-for attribute on a <span> tag to display a default error message.
using System.ComponentModel.DataAnnotations;public class Country{[StringLength(2, MinimumLength = 2)]public string ContinentCode { get; set; }}
The [Range] attribute specifies the minimum and maximum values in a data field. The model will not be flagged as valid if these restrictions are exceeded. In this case, Population must be greater than 0 and less than the big number!
In a Razor page, the @Html.DisplayFor() helper only shows the data in the field. The client-side JavaScript validation scripts use the asp-validation-for attribute on a <span> tag to display a default error message.
using System.ComponentModel.DataAnnotations;public class Country{[Range(1, 10000000000)]public int? Population { get; set; }}