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
4 Answers
Reset to default 2you '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
版权声明:本文标题:c# - The value 'XXYYZZZZ" is not valid for DateTime variable - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741219933a2360776.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论