admin管理员组

文章数量:1276738

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

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; }
}
Share Improve this question edited Mar 31, 2014 at 9:12 szpic asked Mar 31, 2014 at 9:04 szpicszpic 4,49815 gold badges56 silver badges90 bronze badges 6
  • 1 "dd'/'M'/'yyyy" looks a little dubious to me. Are those single quotes correct? – Cerbrus Commented Mar 31, 2014 at 9:05
  • @RajaDani: That wouldn't work, as m is minutes, not months. – Jon Skeet Commented Mar 31, 2014 at 9:08
  • @Cerbrus single quotes are escape mark for slashes. Without them the date string is as follows 31-03-2014 – szpic Commented Mar 31, 2014 at 9:09
  • dd/MM/yyyy will not work. use MM/dd/yyyy.it will work – Manish Parakhiya Commented Mar 31, 2014 at 9:10
  • @Jon Skeet, plz try this "dd/MM/yyyy". – Raja Danish Commented Apr 2, 2014 at 3:56
 |  Show 1 more ment

4 Answers 4

Reset to default 2

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.

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; }
}
DateTime.Now.ToString("dd/MM/yyyy")

"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

本文标签: cThe value 39XXYYZZZZquot is not valid for DateTime variableStack Overflow