Note that there are some explanatory texts on larger screens.

plurals
  1. PODataType attribute wrecking jQuery datepicker on Datetime field
    primarykey
    data
    text
    <p>I am using <em>MVC 4</em> and <em>Razor views</em> and am having trouble understanding why the Edit view on my Date field is not binding properly to the built in jQuery datepicker.</p> <p>The field is of datatype <code>Date</code> in the Database, and <code>DateTime</code> in the domain model. I do not wish to show time, only the date. The field is required and needs formatting.</p> <p>My view looks like this :</p> <pre><code>&lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.Appointment.EndDate) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.Appointment.EndDate) @Html.ValidationMessageFor(model =&gt; model.Appointment.EndDate) &lt;/div&gt; </code></pre> <p>If my metadata on this field is</p> <pre><code>[Required(ErrorMessage = "End Date must be a valid date, like 05/13/2016.")] [DataType(DataType.Date)] [Display(Name = "End Date")] [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")] public object EndDate { get; set; } </code></pre> <p>then my editor output on edit.cshtml is this:</p> <pre><code>&lt;input class="text-box single-line" data-val="true" data-val-date="The field End Date must be a date." data-val-required="End Date must be a valid date, like 05/13/2016." id="Appointment_EndDate" name="Appointment.EndDate" type="date" value="05/09/2013"&gt; </code></pre> <p>this draws a jQuery datepicker BUT WITHOUT SETTING THE VALUE.</p> <p>If I remove the DataType attribute from my metadata and leave everything else the same:</p> <pre><code>[Required(ErrorMessage = "End Date must be a valid date, like 05/13/2016.")] [Display(Name = "End Date")] [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")] public object EndDate { get; set; } </code></pre> <p>then my editor output is this:</p> <pre><code>&lt;input class="text-box single-line valid" data-val="true" data-val-date="The field End Date must be a date." data-val-required="End Date must be a valid date, like 05/13/2016." id="Appointment_EndDate" name="Appointment.EndDate" **type="datetime"** value="05/09/2013"&gt; </code></pre> <p>this draws a plain text box (no datepicker) with the value properly set.</p> <p>I would like to use the datepicker but set the correct value, how can I do this?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload