title: C# MVC 4 ViewModel not accepting null DateTime
body: <p>I'm in a bit of trouble here learning C# and mvc4.</p>
  
  <p>The Problem occurs in the Filter part of my application. 
  I have an ViewModel that grabs the list of "Listar_Produtos" of the database, and some fields for searching options.</p>
  
  <p>What I intend to do is make the filter accept any field, even if it's null values. Because i'll make the filter based on these paramters.</p>
  
  <p>I Have an Viewmodel:</p>
  
  <pre><code>using Foolproof;
  using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.ComponentModel.DataAnnotations;
  using System.Linq;
  using System.Web;
  
  namespace Ecommerce.Models.Repository
  {
      public class Produto_Repository
      {
          public class Index_Listar_Produtos
          {
              public List&lt;Listar_Produto&gt; Index_List_Produto { get; set; }
  
              [Display(Name = "Data de Cadastro Inicial")]
              [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
              public Nullable&lt;DateTime&gt; CadastroInicialData { get; set; }
  
              [Display(Name = "Data de Cadastro Final")]
              [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
              [GreaterThanOrEqualTo("CadastroInicialData", ErrorMessage = "\"Data Inicial\", deve ser maior que \"Data Final\"")]
              public Nullable&lt;DateTime&gt; CadastroFinalData { get; set; }
          }
      }
  }
  </code></pre>
  
  <p>And I have the following View:</p>
  
  <pre><code>&lt;td&gt;
      @Html.LabelFor(Model =&gt; Model.CadastroInicialData)&lt;br /&gt;
      @Html.TextBoxFor(Model =&gt; Model.CadastroInicialData, "{0:dd/MM/yyyy}")
      @Html.ValidationMessageFor(Model =&gt; Model.CadastroInicialData)
  &lt;/td&gt;
  &lt;td&gt;
      @Html.LabelFor(Model =&gt; Model.CadastroFinalData)&lt;br /&gt;
      @Html.TextBoxFor(Model =&gt; Model.CadastroFinalData, "{0:dd/MM/yyyy}")
      @Html.ValidationMessageFor(Model =&gt; Model.CadastroFinalData)
  &lt;/td&gt;
  </code></pre>
  
  <p>In my Controller I have:</p>
  
  <pre><code>[AcceptVerbs(HttpVerbs.Get)]
  public ActionResult Filtro(Produto_Repository.Index_Listar_Produtos ViewModel)
  {
      if (!ModelState.IsValid)
      {
          Produto_Repository.Index_Listar_Produtos Model_list = Produto_Repository.GetProdutoByAll();
          ViewModel.Index_List_Produto = Model_list.Index_List_Produto;
          return View("Index", ViewModel);
      }
  }
  </code></pre>
  
  <p>Where "Produto_Repository.GetProdutoByAll();" returns the list of "Produtos" again.</p>
  
  <p>The code works fine and well if I provide dates in the form. The dates are in "pt-BR" format: 23/03/2013.</p>
  
  <p>But if I provide nothing in the Fields (both Datefields in my View), than the "if(!ModelState.IsValid)" returns true and enters the "if", because both "CadastroInicialData" and "CadastroFinalData" comes with null values</p>
  
  <p>The desired behavior is that the ViewModel could accept null or empty values that are granted by "Nullable" or "DateTime?".</p>
  
  <p>I Tryed to insert values to the nullable date fields doing the following:</p>
  
  <pre><code>if (ViewModel.CadastroInicialData == null)
      ViewModel.CadastroInicialData = Convert.ToDateTime("01/01/2013");
  if (ViewModel.CadastroFinalData == null)
      ViewModel.CadastroFinalData = Convert.ToDateTime("01/01/2013");
  </code></pre>
  
  <p>But now the ViewModel returns the following error:
  "is an invalid date format"</p>
  
  <p>One note is that i'm using the following 'solution' for converting datetimes for pt-BR dateformat on the following question:
  <a href="https://stackoverflow.com/questions/11272851/format-datetime-in-asp-net-mvc-4">Format datetime in asp.net mvc 4</a></p>
  
  <p>How I make the ViewModel accept null values when the text fields are not filled with dates?
  I'm kinda confused here. I apreciate any help ! Thanks !</p>