On my webpage, I have an input where I keep the current date:
@Html.TextBoxFor(model => model.CreationDate, new { @class = "form-control datepicker", @Value = DateTime.Now.ToString("dd'/'M'/'yyyy"), type = "datetime" })
which translates to:
<input value="31/3/2014" class="form-control datepicker" data-val="true" data-val-date="The field CreationDate must be a date." data-val-required="Data jest wymagana" id="CreationDate" name="CreationDate" type="datetime">
As you can see, the default data is: 31/3/2014
But when I try to send it to my method using Ajax, I got an error from ModelState:
The value "31/3/2014" is not valid for CreationDate.
and this is what is in the model:
+ CreationDate {0001-01-01 00:00:00} System.DateTime
This entry field also sets bootstrap datepicker:
$('.datepicker').datepicker({
format: "dd/MM/yyyy",
language: "pl",
autoclose: true,
todayHighlight: true
});
which returns data like:
"31/Marzec/2014"
and with the data above ModelState.Valid=true
but it is necessary that the default input data is valid. The user should use the Datepicker only when he needs to change data other than the current date.
How do I change my code? I tried messing with formats in toString()no effects.
Here is my model:
public class CreateDeviceInstance
{
public int Id { get; set; }
public int DeviceId { get; set; }
public string SerialNo { get; set; }
[Required(ErrorMessage="Data jest wymagana")]
public System.DateTime CreationDate { get; set; }
public Nullable<int> ProjectId { get; set; }
public bool Issue { get; set; }
public string IssueDetails { get; set; }
public Nullable<int> StorageId { get; set; }
public bool MeAsUser { get; set; }
}