Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC input DateTime

Tags:

asp.net-mvc

If I use EditFor in MVC my DateTime field show a not formated datetime, If I use a old school html my field don't receive the error class.

<div class="editor-field">
     <input type="text" name="EstimateTime" id="EstimateTime" value="<%: (Model != null) ? Model.EstimateTime.ToString("dd/MM/yyyy hh:mm:ss tt") : "" %>" />
     <%: Html.TextBoxFor(model => model.EstimateTime, new { @value = (Model != null) ? Model.EstimateTime.ToString("dd/MM/yyyy hh:mm:ss tt") : "" })%>
     <%: Html.ValidationMessageFor(model => model.EstimateTime) %>
</div>

result HTML: Look the difference between the value:

<div class="editor-field">
    <input type="text" name="EstimateTime" id="EstimateTime" value="31/10/2013 01:54:42 PM" class="hasDatepicker">
    <input  id="EstimateTime" name="EstimateTime" type="text" value="10/31/2013 1:54:42 PM" class="input-validation-error text-box single-line">
    <span class="field-validation-error">Isn't a date/time valid</span>
</div>

What is the best practices to fix it?

like image 767
AFetter Avatar asked Mar 03 '26 11:03

AFetter


1 Answers

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd}")]
[DataType(DataType.Date)]
public System.DateTime EstimateTime { get; set; }

This is working for me in the latest version of Chrome

like image 93
Chris Poulter Avatar answered Mar 06 '26 01:03

Chris Poulter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!