To mark a model property mandatory, we use [Required]
attribute.
/Models/PersonalDetail.cs
using System; using System.Collections.Generic;
using System.Linq; using System.Web; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema;
namespace WebApplication1.Models { public class PersonalDetail { [Key] [DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOpt ion.Identity)] public int AutoId { get; set; }
[StringLength(20, MinimumLength = 4, ErrorMessage = "Must be at least 4 characters long.")]
public string FirstName { get; set; }
[Required(ErrorMessage = "Please write your LastName")] public string LastName { get; set; }
[Required]
public int Age { get; set; } [Display(Name = "Is Active?")]
public bool Active { get; set; } } }
Notice the highlighted attribute at the top.