Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You shouldn't be overriding the value of the date input with the same date in a different format, there is no reason to and it will only cause headaches. From my experience you're better off leaving it as is.</p> <pre><code>@Html.TextBoxFor(_item =&gt; _item.mCardOfTheDay.mDateCreated, new { @selectedDate = Model.mCardOfTheDay.mDateCreated, @class = "datePick", @type = "date", @id = "dateCreated" }) </code></pre> <p>What you need to do however is configure the datepicker properly so that it knows how to interpret the date coming from your database. If you want to show a different format to the user than the one posted back to the server, you can make use of the <code>altField</code> (<a href="http://api.jqueryui.com/datepicker/#option-altField" rel="nofollow">source</a>) and <code>altFormat</code> (<a href="http://api.jqueryui.com/datepicker/#option-altFormat" rel="nofollow">source</a>) option. </p> <p>That will allow you to use <code>dateFormat</code> to display the date in whichever format you like in the input to the user, while keeping the posted value in the right format.</p> <p>You won't need this anymore :</p> <pre><code>$('.datePick').each(function () { var a = $(this).datepicker({ dateFormat: "dd/ww/yy", defaultDate: new Date($(this).val()) }); }); </code></pre> <p>You'll also need to get rid of <code>ApplyFormatInEditMode = true</code> on your model property attributes. Let the framework handle dates based on current culture, don't try working against it by applying formats all over the place.</p> <p>Just leave the textbox value be whatever your database sends, and use the datapicker's functionality to do what you need on the client side.</p> <p>The only time I would advise overriding the value is if you're worried about the input showing the time part when you only want the date. In that case, you should do it like so :</p> <pre><code>@Html.TextBoxFor(_item =&gt; _item.mCardOfTheDay.mDateCreated, new { @selectedDate = Model.mCardOfTheDay.mDateCreated, @class = "datePick", @type = "date", @id = "dateCreated", @Value = Model.mCardOfTheDay.mDateCreated.ToString("mm/d/yyyy") }) </code></pre> <p>Notice <code>@Value</code> with a capital v, that is actually important and not a typo. You then pass a date format to the <code>ToString</code> method, but it needs to be the same format as your database.</p> <hr> <p><strong>Edit:</strong></p> <p>I went ahead and built a small test-case to try and replicate your issue. Here's what I have :</p> <p>Controller :</p> <pre><code>public ActionResult Test() { return View(new TestViewModel() { DateCreated = DateTime.Now.AddDays(10) }); } [HttpPost] public ActionResult Test(TestViewModel model) { return RedirectToAction("Test"); } </code></pre> <p>ViewModel :</p> <pre><code>public class TestViewModel { public DateTime DateCreated { get; set; } } </code></pre> <p>View :</p> <pre><code>@using (Html.BeginForm()) { @Html.TextBoxFor(m =&gt; m.DateCreated, new { @type = "date", @Value = Model.DateCreated.ToString("d") }) &lt;input type="submit"&gt; } // This is the rendered input &lt;input Value="12/1/2013" data-val="true" data-val-date="The field DateCreated must be a date." id="DateCreated" name="DateCreated" type="date" value="12/1/2013 4:03:19 PM" /&gt; </code></pre> <p>JS:</p> <pre><code>$('input[type="date"]').datepicker(); </code></pre> <p>This all works as expected; datepicker has the date in the input's value selected by default, value of <code>model.DateCreated</code> is set to a <code>DateTime</code> object on post. Notice I haven't messed with formats anywhere but in the display value where I replaced the full date with the standard short date pattern.</p> <p>Can you try simplifying your code to match what I have and let me know what happens?</p>
    singulars
    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.
    1. VO
      singulars
      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