title: ASP.NET Custom ErrorMessage for Model Enum field
body: <p>I am developing a website built on EntityFrameworkCore and targeting ASP.NET Core 2.1.  I want to specify an error message for an enum field in my model like so:</p>
  
  <pre class="lang-cs prettyprint-override"><code>[Required(ErrorMessage = "Select an item from the list.")]
  public MyEnum MyEnum { get; set; }
  </code></pre>
  
  <p>However, the stock message is still generated: <code>The value '0' is invalid</code>.  The problem appears to be that the Enum type is validated prior to any of my code being evaluated.  The two approaches presented here (<a href="https://www.codeproject.com/Articles/1204077/ASP-NET-Core-MVC-Model-Validation" rel="nofollow noreferrer">https://www.codeproject.com/Articles/1204077/ASP-NET-Core-MVC-Model-Validation</a>), either creating a class that inherits from ValidationAttribute, or having the model inherit from IValidatableObject both suffer from this.</p>
  
  <p>I have found a workaround: declare the field as an int, and then use a custom validation attribute:</p>
  
  <pre class="lang-cs prettyprint-override"><code>[EnumCheck(typeof(MyEnum), ErrorMessage = "Select an item form the list.")]
  public int MyEnum { get; set; }
  </code></pre>
  
  <p>...and then subclass from ValidationAttribute:</p>
  
  <pre class="lang-cs prettyprint-override"><code>sealed public class EnumCheck : ValidationAttribute
  {
      readonly Type t_;
  
      public EnumCheck(Type t)
      {
          t_ = t;
      }
  
      public override bool IsValid(object value)
      {
          return Enum.IsDefined(t_, value);
      }
  }
  </code></pre>
  
  <p>This approach has some drawbacks as now I need to cast the field to the Enum type in many places that it is used.</p>
  
  <p>Is there a way to provide an ErrorMessage for Enum field types?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>The following is a minimal example (No longer using EnumCheck subclass from ValidationAttribute, but rather the EnumDataType mentioned by @PéterCsajtai):</p>
  
  <p>Model
  </p>
  
  <pre><code>namespace web.Models
  {
      public enum Day
      {
          Sunday = 1,
          Monday,
          Tuesday,
          Wednesday,
          Thursday,
          Friday,
          Saturday
      }
  
      public class Form
      {
          [EnumDataType(typeof(Day), ErrorMessage = "Select an item from the list.")]
          public Day Day { get; set; }
      }
  }
  </code></pre>
  
  <p>Controller
  </p>
  
  <pre><code>namespace web.Controllers
  {
      public class HomeController : Controller
      {
          public IActionResult Index()
          {
              return View();
          }
  
          public IActionResult Save(Form model)
          {
              if(!ModelState.IsValid)
              {
                  return View("Index");
              }
  
              return View("Index", model);
          }
      }
  }
  </code></pre>
  
  <p>View
  </p>
  
  <pre><code>&lt;form asp-controller="Home"&gt;
      &lt;div asp-validation-summary="All" class="text-danger"&gt;&lt;/div&gt;
      &lt;fieldset&gt;
          &lt;label asp-for="@Model.Day"&gt;&lt;/label&gt;
          &lt;select asp-for="@Model.Day" asp-items="Html.GetEnumSelectList&lt;Day&gt;()"&gt;
              &lt;option value=""&gt;Select...&lt;/option&gt;
          &lt;/select&gt;
          @Html.ValidationMessageFor(m =&gt; m.Day)
          &lt;span asp-validation-for="@Model.Day" class="text-danger"&gt;&lt;/span&gt;
      &lt;/fieldset&gt;
      &lt;fieldset&gt;
          &lt;input type="submit" asp-action="Save" /&gt;
      &lt;/fieldset&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>And the output after form post:</p>
  
  <p><a href="https://i.stack.imgur.com/9s3k0.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9s3k0.png" alt="form output"></a></p>