On my webpage I have input where I keep current date:
@Html.TextBoxFor(model => model.CreationDate, new { @class = "form-control datepicker", @Value = DateTime.Now.ToString("dd'/'M'/'yyyy"), type = "datetime" })
which renders into:
<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'm trying to send it to my method using Ajax I got error from ModelState
:
The value "31/3/2014" is not valid for CreationDate.
and this is what is in model:
+ CreationDate {0001-01-01 00:00:00} System.DateTime
To this input box is also set bootstrap datepicker
:
$('.datepicker').datepicker({
format: "dd/MM/yyyy",
language: "pl",
autoclose: true,
todayHighlight: true
});
which returns data like:
"31/Marzec/2014"
and with data above the ModelState.Valid=true
but there is need that input box default data should be valid. User should use Datepicker only when he needs to change to data other that current date.
How should I modify my code? I tryied messing with Formats in toString()
with no effects.
Here 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; }
}
you 're using "dd'/'M'/'yyyy"
format initially,
(DateTime.Now.ToString("dd'/'M'/'yyyy"))
and "dd/MM/yyyy"
format later,
format: "dd/MM/yyyy"
this is not good.
Use the same format (I suggest "dd/MM/yyyy"
) in both cases.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With