szpic
szpic

Reputation: 4496

The value 'XX/YY/ZZZZ" is not valid for DateTime variable

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; }
}

Upvotes: 7

Views: 25009

Answers (4)

Andile Ngubane
Andile Ngubane

Reputation: 1

"31/Marzec/2014" format : "dd/MM/yyyy"; do not use capital letters for specifying a month. The date format in bootstrap datepicker format : "dd/mm/yyyy" your result will change 31/03/2014

Upvotes: 0

Shajeer Puzhakkal
Shajeer Puzhakkal

Reputation: 161

DateTime.Now.ToString("dd/MM/yyyy")

Upvotes: 0

Rolwin Crasta
Rolwin Crasta

Reputation: 4339

Try this

public class CreateDeviceInstance 
{    
    public int Id { get; set; }
    public int DeviceId { get; set; }
    public string SerialNo { get; set; }
    [Required(ErrorMessage="Data jest wymagana")]
    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
    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; }
}

Upvotes: 0

Emanuele Greco
Emanuele Greco

Reputation: 12721

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.

Upvotes: 2

Related Questions