在我的网页上,我输入了保持当前日期的地方:
@Html.TextBoxFor(model => model.CreationDate, new { @class = "form-control datepicker", @Value = DateTime.Now.ToString("dd'/'M'/'yyyy"), type = "datetime" })
呈现:
<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">
如您所见,默认数据为31/3/2014
但是当我尝试使用Ajax将其发送到我的方法时,我收到来自ModelState
的错误:
The value "31/3/2014" is not valid for CreationDate.
这就是模型中的内容:
+ CreationDate {0001-01-01 00:00:00} System.DateTime
此输入框也设置为bootstrap datepicker
:
$('.datepicker').datepicker({
format: "dd/MM/yyyy",
language: "pl",
autoclose: true,
todayHighlight: true
});
返回如下数据:
"31/Marzec/2014"
以及ModelState.Valid=true
但是输入框默认数据需要有效。用户只有在需要更改当前日期以外的数据时才应使用Datepicker。
我应该如何修改我的代码?我试图在toString()
中弄乱格式而没有任何影响。
这是我的模特:
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; }
}
答案 0 :(得分:2)
您最初使用"dd'/'M'/'yyyy"
格式,
(DateTime.Now.ToString("dd'/'M'/'yyyy"))
和"dd/MM/yyyy"
格式稍后,
format: "dd/MM/yyyy"
这不好
在这两种情况下都使用相同的格式(我建议"dd/MM/yyyy"
)。
答案 1 :(得分:0)
试试这个
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; }
}
答案 2 :(得分:0)
DateTime.Now.ToString("dd/MM/yyyy")
答案 3 :(得分:0)
“31 / Marzec / 2014”格式:“dd / MM / yyyy”;不要使用大写字母来指定月份。 bootstrap datepicker格式的日期格式:“dd / mm / yyyy”您的结果将更改31/03/2014