Date creation date in jQuery MVC format is not set only after using it

I have the following datepicker which still works just fine:

$('.tmmdatepicker').datepicker({ showOn: "button", buttonImage: "../Content/images/ico-calendar.png", buttonImageOnly: true, dateFormat: "d M, yy", showAnim: "fadeIn", autosize:true }); 

Here is a view of a razor:

 <tr> <td>Start date:</td> <td> @Html.EditorFor(m => m.CurrentFilter.StartTime, new { @class = "tmmdatepicker" , @style = "width:80px" }) </td> 

It still works well, but the first time the view is loaded, the time will not be formatted in this format (d M, yy), only after I use the datetimepicker once, then the contents of EditorFor will be updated using the rule.

So, when I first enter the site:

(http://i.imgur.com/nAm56.png)

and after I use the button to select a date:

enter image description here <--- this is how I want to see him at the beginning.

So, is there a way to load this behavior at the beginning? (sorry, I canโ€™t insert direct links to photos)

+4
source share
2 answers

You can use EditorTemplates.

Create a ~/Views/Shared/EditorTemplates/DateTime.cshtml file with the following codes:

 @model DateTime? @Html.TextBox("", (Model.HasValue ? Model.Value.ToString("dd MMM, yyyy") : string.Empty)) 
+2
source

The problem is that you are specifying the datetime format on the client side and it seems you have completely forgotten what is happening on the server. The first time the view loads a value, it is displayed by the asp.net viewer, which converts the date to a string using the current UI culture information for your project. You can change the culture of the entire project or create an editor template for the DateTime type, as already suggested in one of the answers.

+1
source

Source: https://habr.com/ru/post/1447238/


All Articles