title: asp.net core select validation with data annotation not working
body: <p>I have spent a remarkable amount of time to find out but I couldn't. When I post my model without selecting any option - <em>in fact selecting <code>0 - Select</code> - option</em>, the <code>Required</code> validation is not working. </p>
  
  <p>I also tried removing the programatically added default option from service code to the view code, validation has not worked like this as well. </p>
  
  <p>If I removed the default options completely, then view engine automatically selects the first option from the list and the validation is never be done.</p>
  
  <p>How can I make server side validation done properly?</p>
  
  <p>This is my <strong>model</strong></p>
  
  <pre><code>public class AuditViewModel
  {
      public Guid Id { get; set; }
  
      [Display(Name = "Subject")]
      [Required(ErrorMessage = "IsRequired")]
      public string Subject { get; set; }
  
      public string AuditType { get; set; }
  
      public string LocationCountry { get; set; }
  
      public string LocationOffice { get; set; }
  
      [Required(ErrorMessage = "IsRequired")]
      [Display(Name = "AuditType")]
      public int AuditTypeId { get; set; }
  
      public string CreatedOn { get; set; }
  
      public string ModifiedOn { get; set; }
  
      public string CreatedBy { get; set; }
  
      [Display(Name = "Description")]
      [Required(ErrorMessage = "IsRequired")]
      public string Description { get; set; }
  
      [Display(Name = "Country")]
      [Required(ErrorMessage = "IsRequired")]
      public int LocationCountryId { get; set; }
  
      [Display(Name = "Office")]
      [Required(ErrorMessage = "IsRequired")]
      public int LocationOfficeId { get; set; }
  
      [Display(Name = "Season")]
      public string Season { get; set; }
  
      public List&lt;SelectListItem&gt; Countries { get; set; }
  
      public List&lt;SelectListItem&gt; AuditTypes { get; set; }
  
      public List&lt;SelectListItem&gt; Offices { get; set; }
  
      public List&lt;AuditViewModel&gt; AuditList { get; set; }
  }
  </code></pre>
  
  <p>This is the <strong>service</strong> that get select list data and creates the lists for binding</p>
  
  <pre><code>public class AuditViewModelService : IAuditViewModelService
  {
      public List&lt;SelectListItem&gt; GetAuditTypes()
      {
          var list = new List&lt;SelectListItem&gt;
              {
                  new SelectListItem { Text = _sharedLocalizer.GetLocalizedString("Select"), Value = "0", Selected = true }
              };
          foreach (AuditType item in Enum.GetValues(typeof(AuditType)))
          {
              list.Add(new SelectListItem { Text = _enumLocalizer.GetLocalizedString(item.ToString()), Value = ((int)item).ToString() });
          }
          return list;
      }
  
      public List&lt;SelectListItem&gt; GetCountries()
      {
          var list = new List&lt;SelectListItem&gt; { new SelectListItem { Text = _sharedLocalizer.GetLocalizedString("Select"), Value = "0", Selected = true } };
          list.AddRange(_countryRepository.GetAll().ToList().ToSelectListItemList("Name"));
          return list;
      }
  
      public List&lt;SelectListItem&gt; GetOffices()
      {
          var list = new List&lt;SelectListItem&gt; { new SelectListItem { Text = _sharedLocalizer.GetLocalizedString("Select"), Value = "0", Selected = true } };
          list.AddRange(_officeRepository.GetAll().ToList().ToSelectListItemList("Name"));
          return list;
      }
  }
  </code></pre>
  
  <p>An this is select inputs part of the <strong>view</strong></p>
  
  <pre><code>&lt;div class="form-group m-form__group"&gt;
      &lt;label asp-for="AuditTypeId"&gt;&lt;/label&gt;
      &lt;select asp-for="AuditTypeId" asp-items="@Model.AuditTypes" class="form-control m-input m-input--square" id="auditTypeId"&gt;
          &lt;option value="0"&gt;Select&lt;/option&gt;
      &lt;/select&gt;
      &lt;span asp-validation-for="AuditTypeId"&gt;&lt;/span&gt;
  &lt;/div&gt;
  &lt;div class="form-group m-form__group"&gt;
      &lt;label asp-for="LocationCountryId"&gt;&lt;/label&gt;
      &lt;select asp-for="LocationCountryId" asp-items="@Model.Countries" class="form-control m-input m-input--square" id="locationCountryId"&gt;
          &lt;option value="0"&gt;Select&lt;/option&gt;
      &lt;/select&gt;
      &lt;span asp-validation-for="LocationCountryId"&gt;&lt;/span&gt;
  &lt;/div&gt;
  &lt;div class="form-group m-form__group"&gt;
      &lt;label asp-for="LocationOfficeId"&gt;&lt;/label&gt;
      &lt;select asp-for="LocationOfficeId" asp-items="@Model.Offices" class="form-control m-input m-input--square" id="locationOfficeId"&gt;
          &lt;option value="0"&gt;Select&lt;/option&gt;
      &lt;/select&gt;
      &lt;span asp-validation-for="LocationOfficeId"&gt;&lt;/span&gt;
  &lt;/div&gt;
  </code></pre>