title: ASP.NET MVC - custom error message based on condition
body: <p>I am trying to create a custom error message based on a condition here. I have date field which is a conditional mandatory field. I want to make that required field based on some condition only.</p>
  
  <p>I have created a method which checks those condition and if it fails then it inserts a error message into the <code>ModelState</code>.</p>
  
  <p>Validator Class</p>
  
  <pre><code>public class Validator
  {
      public Dictionary&lt;string, string&gt; errors { get; set; }
      public bool IsValid { get; set; } = true;
  
      public Validator()
      {
          errors = new Dictionary&lt;string, string&gt;();
      }
  
      public void PatientDetailRules(PatientDetailModel model)
      {
          //Rule 11 - Last sight test - requried on condition
          if (!model.IsFirstTest &amp;&amp; !model.IsTestDateUnknow)
          {
              if (model.DateOfLastSight == DateTime.MinValue)
                  errors.Add(nameof(model.DateOfLastSight), "missing");
          }
       }
  }
  </code></pre>
  
  <p>PatientDetailModel.cs</p>
  
  <pre><code>public class PatientDetailModel
  {
     [Display(Name = "OPH_GOS1_DateOfLastSight", ResourceType = typeof(Ophthalmic))]
      public DateTime DateOfLastSight { get; set; }
  
  }
  </code></pre>
  
  <p>Controller - Action method </p>
  
  <pre><code> [HttpPost]
      public ActionResult SubmitPatientDetails(PatientDetailModel Model)
      {
          Validator vs = new Validator();
          vs.PatientDetailRules(Model);
          if (!vs.IsValid)
          {
              foreach (KeyValuePair&lt;string, string&gt; errorlist in vs.errors)
                  ModelState.AddModelError(errorlist.Key, errorlist.Value);
          }
  
          if (ModelState.IsValid)
          {
              var result = Entities.Ophthalmic.DataAccess.SavePatientDetails(Model);
              return Json(new { result = true }, JsonRequestBehavior.AllowGet);
          }
          return PartialView("~/views/Ophthalmic/Partials/_PatientDetails.cshtml", Model);
       }
  </code></pre>
  
  <p>view -</p>
  
  <pre><code>&lt;div class="col-xs-12 col-sm-12"&gt;
      &lt;div class="margin-top-10"&gt;
          @Html.LabelFor(p =&gt; p.DateOfLastSight)
          &lt;div class="display-flex"&gt;
              @Html.TextBoxFor(m =&gt; m.DateOfLastSight, new { @class = "form-control", autocomplete = "off", placeholder = "dd/mm/yyyy", maxlength = 8, id = "txtLastSightTestDate" })
          &lt;/div&gt;
          @Html.ValidationMessageFor(model =&gt; model.DateOfLastSight, "", new { @class = "text-danger" })
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>But here, I am not sure why. Its giving the error as "The LAST SIGHT TEST DATE is requried." This message is coming from attribute "Data-val-required" which is generating in HTML. But i have not given as required field attribute here.</p>
  
  <p>Also when I make datatype nullable, (Datetime?) in model the nits not giving any error itself.</p>
  
  <p>What am I doing wrong here?</p>
  
  <p>EDIT - I also tried inserting error from Resource file but still not working.</p>
  
  <pre><code>if (!model.IsFirstTest &amp;&amp; !model.IsTestDateUnknow)
          {
              if (model.DateOfLastSight == DateTime.MinValue)
                  errors.Add(nameof(model.DateOfLastSight), Resources.Ophthalmic.OPH_GOS1_DateOfLastSightMissingError);
          }
  </code></pre>