我一直在此停留了一個小時。DateTime動態字段無法正常工作
我有一個動態表單。添加新項目時,工作正常,
但後來我嘗試提交編輯表單我得到的客戶端驗證日期時間:
這是HTML:
using (Html.BeginCollectionItem("ValidDates"))
{
<div class="row mt-10">
@Html.HiddenFor(m => m.Id)
@Html.HiddenFor(m => m.IsDeleted, new { data_is_deleted = "false" })
<div class="col-md-5">
@Html.TextBoxFor(m => m.ValidFrom, new { @Value = Model.ValidFrom.ToString("dd/MM/yyyy"), @class = "form-control dateTimePicker" })
@Html.ValidationMessageFor(m => m.ValidFrom)
</div>
<div class="col-md-5">
@Html.TextBoxFor(m => m.ValidTo, new { @Value = Model.ValidFrom.ToString("dd/MM/yyyy"), @class = "form-control dateTimePicker" })
@Html.ValidationMessageFor(m => m.ValidTo)
</div>
<div class="col-md-1">
@Html.CheckBoxFor(m => m.Enabled)
</div>
<div class="col-md-1">
<span class="glyphicon glyphicon-trash" data-action="removeItem" title="remove" style="cursor:pointer"></span>
</div>
</div>
}
這是型號:
public class BucketValidDates : BaseEntity
{
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime ValidFrom { get; set; }
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime ValidTo { get; set; }
public bool Enabled { get; set; }
public virtual Bucket Bucket { get; set; }
[NotMapped]
public bool IsDeleted { get; set; }
}
我甚至TR ied使用EditorFor
(這工作時,我張貼回來但日期格式不正確)。
using (Html.BeginCollectionItem("ValidDates"))
{
<div class="row mt-10">
@Html.HiddenFor(m => m.Id)
@Html.HiddenFor(m => m.IsDeleted, new { data_is_deleted = "false" })
<div class="col-md-5">
@Html.EditorFor(m => m.ValidFrom, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.ValidFrom)
</div>
<div class="col-md-5">
@Html.EditorFor(m => m.ValidTo, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.ValidTo)
</div>
<div class="col-md-1">
@Html.CheckBoxFor(m => m.Enabled)
</div>
<div class="col-md-1">
<span class="glyphicon glyphicon-trash" data-action="removeItem" title="remove" style="cursor:pointer"></span>
</div>
</div>
}
我試圖改變格式模型{0:DD/MM/YYYY}
我需要的格式是:day/Month/Year
但你可以在圖片查看下面的格式爲Year/Month/day
的DatePicker的該項目是:http://eonasdan.github.io/bootstrap-datetimepicker/
這個約會是否與此有關?
您當然應該在DisplayFormat設爲dd-MM-yyyy(除非有不同的原因)。 –
@NathanKoop I h大道試圖將其設置爲dd-MM-yyyy,但我仍然得到相同的格式在查看 –
檢查這篇文章http://stackoverflow.com/questions/18546971/mvc-4-how-to-validate-a-non- us-date-with-client-validation – ssimeonov