title: ASP.NET MVC: Validating start and end dates
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/7025198/mvc3-custom-validation-compare-two-dates">MVC3 custom validation: compare two dates</a>  </p>
  </blockquote>
  
  
  
  <p>I am trying  to make a validation for my date so that start date must be &lt; than end date.If end date is before start date tha n i throw an exception .How can i show this exception on my screen so when i press search buton and the dates are wrong a message to appear?</p>
  
  <pre><code>    public ActionResult SearchFree(DateTime? StartDate, DateTime? EndDate)
      {
  
          if (StartDate.HasValue &amp;&amp; EndDate.HasValue)
          {
          DateTime d1 = StartDate.Value;
          DateTime d2 = EndDate.Value;
          TimeSpan span = d2 - d1;
  
  
          if (span.Days &lt;= 0)
          {
              throw new ArgumentOutOfRangeException("start date must be before end date");
          }
  
          try
          {
              DBContext.Current.Open();
              var model = Reservation.SelectFreeRooms(StartDate, EndDate);
              DBContext.Current.Close();
              return View(model);
          }
          catch (ArgumentException ae)
          {
              throw ae;
          }
  
  
                }
        return View(new List&lt;dynamic&gt;());
      }
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/IKXKt.png" alt="enter image description here"></p>